{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "import DeepQuantum以及相关的库。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import deepquantum as dq\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import numpy as np"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本的量子门\n",
    "\n",
    "所有的Gate都是Operation的子类，都有布尔变量`den_mat`和`tsr_mode`。\n",
    "\n",
    "参数`den_mat`表示这个操作是处理密度矩阵，还是态矢。\n",
    "\n",
    "参数`tsr_mode`表示这个操作的输入输出是张量态（形状为（batch, 2, ..., 2）的tensor），还是态矢或密度矩阵。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们利用`QubitState`准备一个单比特量子态（默认为'zeros'表示全0态，此外，有'equal'表示等权叠加态，'ghz'表示GHZ态），其数据是torch的tensor，存在属性`state`中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.+0.j],\n",
      "        [1.+0.j]])\n"
     ]
    }
   ],
   "source": [
    "qstate = dq.QubitState(nqubit=1, state=[0,1])\n",
    "state1 = qstate.state\n",
    "print(state1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实例化一个单比特量子门，可以从`matrix`属性获得量子门的基础表示（相对于指定任意控制位的量子门而言）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.+0.j, 1.+0.j],\n",
       "        [1.+0.j, 0.+0.j]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = dq.PauliX()\n",
    "x.matrix"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用量子门对量子态进行操作，既可以通过手动的矩阵乘法，也可以通过把量子门直接作用在量子态上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.+0.j],\n",
      "        [0.+0.j]])\n",
      "tensor([[1.+0.j],\n",
      "        [0.+0.j]])\n"
     ]
    }
   ],
   "source": [
    "print(x.matrix @ state1)\n",
    "print(x(state1))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们再看带参数的量子门的例子。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]])\n",
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]])\n"
     ]
    }
   ],
   "source": [
    "rx = dq.Rx(torch.pi/2)\n",
    "print(rx.matrix @ state1)\n",
    "print(rx(state1))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以利用`get_matrix()`获取带参数量子门的计算过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.0000e+00-1.j],\n",
       "        [-4.3711e-08+0.j]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rx.get_matrix(torch.pi) @ state1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意`matrix`只是纯粹的矩阵表示，因此当需要记录计算图来求参数的梯度时，必须使用`update_matrix()`或`get_matrix()`，区别在于前者使用该量子门本身的参数，而后者需要输入参数，适用于外部的指定参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.7071+0.0000j, -0.0000-0.7071j],\n",
      "        [-0.0000-0.7071j, 0.7071+0.0000j]])\n",
      "tensor([[0.7071+0.0000j, -0.0000-0.7071j],\n",
      "        [-0.0000-0.7071j, 0.7071+0.0000j]], grad_fn=<ReshapeAliasBackward0>)\n",
      "tensor([[0.7071+0.0000j, -0.0000-0.7071j],\n",
      "        [-0.0000-0.7071j, 0.7071+0.0000j]], grad_fn=<ReshapeAliasBackward0>)\n"
     ]
    }
   ],
   "source": [
    "rx = dq.Rx(torch.pi/2, requires_grad=True)\n",
    "theta = nn.Parameter(torch.tensor(torch.pi/2))\n",
    "print(rx.matrix)\n",
    "print(rx.update_matrix())\n",
    "print(rx.get_matrix(theta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]])\n",
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]], grad_fn=<MmBackward0>)\n",
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]], grad_fn=<MmBackward0>)\n",
      "tensor([[0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]], grad_fn=<SqueezeBackward1>)\n"
     ]
    }
   ],
   "source": [
    "print(rx.matrix @ state1)\n",
    "print(rx.update_matrix() @ state1)\n",
    "print(rx.get_matrix(theta) @ state1)\n",
    "print(rx(state1))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "处理多比特量子态时，量子门的`nqubit`需要与量子态的一致，用`wires`来指定量子门作用于哪些线路上。同样，既可以通过`get_unitary()`得到完整的酉矩阵后进行手动的矩阵乘法，也可以通过把量子门直接作用在量子态上。需要注意，前者的计算效率远低于后者。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [1.+0.j]])\n",
      "tensor([[0.0000+0.0000j],\n",
      "        [0.0000+0.0000j],\n",
      "        [0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]], grad_fn=<MmBackward0>)\n",
      "tensor([[0.0000+0.0000j],\n",
      "        [0.0000+0.0000j],\n",
      "        [0.0000-0.7071j],\n",
      "        [0.7071+0.0000j]], grad_fn=<SqueezeBackward1>)\n"
     ]
    }
   ],
   "source": [
    "state2 = dq.QubitState(nqubit=2, state=[0,0,0,1]).state\n",
    "print(state2)\n",
    "rx = dq.Rx(torch.pi/2, nqubit=2, wires=[1], requires_grad=True)\n",
    "print(rx.get_unitary() @ state2)\n",
    "print(rx(state2))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DeepQuantum中几乎所有的量子门都支持额外指定任意多的控制位（例外有`CNOT`、`Toffoli`、`Fredkin`等）。因此，同一个量子门可能会有不止一种调用方法。比如cnot和Toffoli可以用`PauliX`实现，Fredkin可以用`Swap`实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "cx1 = dq.CNOT(wires=[0,1])\n",
    "cx2 = dq.PauliX(nqubit=2, wires=[1], controls=[0])\n",
    "\n",
    "ccx1 = dq.Toffoli(wires=[0,1,2])\n",
    "ccx2 = dq.PauliX(nqubit=3, wires=[2], controls=[0,1])\n",
    "\n",
    "cswap1 = dq.Fredkin(wires=[0,1,2])\n",
    "cswap2 = dq.Swap(nqubit=3, wires=[1,2], controls=[0])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意，它们的`matrix`是不同的。并且，前者的计算效率略高于后者。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])\n",
      "tensor([[0.+0.j, 1.+0.j],\n",
      "        [1.+0.j, 0.+0.j]])\n",
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])\n",
      "tensor([[0.+0.j, 1.+0.j],\n",
      "        [1.+0.j, 0.+0.j]])\n",
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])\n",
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])\n"
     ]
    }
   ],
   "source": [
    "print(cx1.matrix)\n",
    "print(cx2.matrix)\n",
    "\n",
    "print(ccx1.matrix)\n",
    "print(ccx2.matrix)\n",
    "\n",
    "print(cswap1.matrix)\n",
    "print(cswap2.matrix)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "后者可以用`get_unitary()`来检查。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])\n",
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])\n",
      "tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])\n"
     ]
    }
   ],
   "source": [
    "print(cx2.get_unitary())\n",
    "print(ccx2.get_unitary())\n",
    "print(cswap2.get_unitary())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用户还可以通过`UAnyGate`来封装任意酉矩阵。比如，把$4\\times4$的酉矩阵作用在三比特量子态的后两个量子比特上，其中用`minmax`指定作用范围，需要和酉矩阵的大小匹配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]])\n"
     ]
    }
   ],
   "source": [
    "unitary = [[0, 0, 0, 1],\n",
    "           [0, 0, 1, 0],\n",
    "           [1, 0, 0, 0],\n",
    "           [0, 1, 0, 0]]\n",
    "u = dq.UAnyGate(unitary=unitary, nqubit=3, minmax=[1,2])\n",
    "print(u.get_unitary())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 制备GHZ态\n",
    "\n",
    "我们来实现一下制备GHZ态（$|\\psi\\rangle = \\left(|000\\rangle+|111\\rangle\\right)/\\sqrt{2}$）这个经典的例子。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.7071+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.7071+0.j]])\n"
     ]
    }
   ],
   "source": [
    "state3 = dq.QubitState(3).state\n",
    "h = dq.Hadamard(nqubit=3, wires=0)\n",
    "cx1 = dq.CNOT(nqubit=3, wires=[0,1])\n",
    "cx2 = dq.CNOT(nqubit=3, wires=[0,2])\n",
    "print(cx2(cx1(h(state3))))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 量子线路：QubitCircuit\n",
    "\n",
    "量子线路是DeepQuantum的核心对象。通过`QubitCircuit`进行初始化，然后可以在实例对象上添加各种量子门，最后进行演化和测量。我们把上面这个例子用量子线路来实现。并且可以对线路进行可视化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.7071+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.0000+0.j],\n",
      "        [0.7071+0.j]])\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"220.141994pt\" height=\"172.003598pt\" viewBox=\"0 0 220.141994 172.003598\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:24.672494</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 172.003598 \nL 220.141994 172.003598 \nL 220.141994 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 208.306595 44.2832 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 208.306595 90.637199 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 208.306595 136.991198 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 135.994356 90.637199 \nL 135.994356 44.2832 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 182.348355 136.991198 \nL 182.348355 44.2832 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 74.575306 59.348249 \nL 104.705406 59.348249 \nL 104.705406 29.21815 \nL 74.575306 29.21815 \nz\n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 135.994356 48.802714 \nC 137.192945 48.802714 138.342605 48.32651 139.190135 47.478979 \nC 140.037666 46.631448 140.51387 45.481789 140.51387 44.2832 \nC 140.51387 43.08461 140.037666 41.934951 139.190135 41.08742 \nC 138.342605 40.239889 137.192945 39.763685 135.994356 39.763685 \nC 134.795766 39.763685 133.646107 40.239889 132.798576 41.08742 \nC 131.951045 41.934951 131.474841 43.08461 131.474841 44.2832 \nC 131.474841 45.481789 131.951045 46.631448 132.798576 47.478979 \nC 133.646107 48.32651 134.795766 48.802714 135.994356 48.802714 \nz\n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 135.994356 101.182734 \nC 138.791064 101.182734 141.473603 100.07159 143.451175 98.094018 \nC 145.428746 96.116447 146.53989 93.433907 146.53989 90.637199 \nC 146.53989 87.84049 145.428746 85.157951 143.451175 83.18038 \nC 141.473603 81.202808 138.791064 80.091664 135.994356 80.091664 \nC 133.197647 80.091664 130.515108 81.202808 128.537536 83.18038 \nC 126.559965 85.157951 125.448821 87.84049 125.448821 90.637199 \nC 125.448821 93.433907 126.559965 96.116447 128.537536 98.094018 \nC 130.515108 100.07159 133.197647 101.182734 135.994356 101.182734 \nz\n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 182.348355 48.802714 \nC 183.546944 48.802714 184.696604 48.32651 185.544135 47.478979 \nC 186.391665 46.631448 186.86787 45.481789 186.86787 44.2832 \nC 186.86787 43.08461 186.391665 41.934951 185.544135 41.08742 \nC 184.696604 40.239889 183.546944 39.763685 182.348355 39.763685 \nC 181.149766 39.763685 180.000106 40.239889 179.152575 41.08742 \nC 178.305045 41.934951 177.82884 43.08461 177.82884 44.2832 \nC 177.82884 45.481789 178.305045 46.631448 179.152575 47.478979 \nC 180.000106 48.32651 181.149766 48.802714 182.348355 48.802714 \nz\n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 182.348355 147.536733 \nC 185.145063 147.536733 187.827603 146.425589 189.805174 144.448017 \nC 191.782746 142.470446 192.89389 139.787907 192.89389 136.991198 \nC 192.89389 134.19449 191.782746 131.511951 189.805174 129.534379 \nC 187.827603 127.556807 185.145063 126.445663 182.348355 126.445663 \nC 179.551646 126.445663 176.869107 127.556807 174.891536 129.534379 \nC 172.913964 131.511951 171.80282 134.19449 171.80282 136.991198 \nC 171.80282 139.787907 172.913964 142.470446 174.891536 144.448017 \nC 176.869107 146.425589 179.551646 147.536733 182.348355 147.536733 \nz\n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#p056298e4f4)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#p056298e4f4)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#p056298e4f4)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#p056298e4f4)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 47.870387) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-48\" d=\"M 628 4666 \nL 1259 4666 \nL 1259 2753 \nL 3553 2753 \nL 3553 4666 \nL 4184 4666 \nL 4184 0 \nL 3553 0 \nL 3553 2222 \nL 1259 2222 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 135.994356 96.663219 \nL 135.994356 84.611179 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 129.968336 90.637199 \nL 142.020375 90.637199 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 182.348355 143.017218 \nL 182.348355 130.965178 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 176.322335 136.991198 \nL 188.374375 136.991198 \n\" clip-path=\"url(#p056298e4f4)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p056298e4f4\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"205.741994\" height=\"157.603598\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 371.107x284.278 with 1 Axes>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(3)\n",
    "cir.h(0)\n",
    "cir.cnot(0, 1)\n",
    "cir.cnot(0, 2)\n",
    "print(cir())\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以对线路进行测量，返回的结果是字典或者字典的列表，字典的key是比特串，value是对应测量到的次数，shots默认为1024。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'111': 532, '000': 492}\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"405.557992pt\" height=\"218.357597pt\" viewBox=\"0 0 405.557992 218.357597\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:25.018621</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 218.357597 \nL 405.557992 218.357597 \nL 405.557992 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 269.030334 172.799663 \nL 281.082374 172.799663 \nL 275.056354 181.491038 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #778899\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 315.384333 172.799663 \nL 327.436373 172.799663 \nL 321.410353 181.491038 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #778899\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 361.738333 172.799663 \nL 373.790372 172.799663 \nL 367.764352 181.491038 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #778899\"/>\n   </g>\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 393.722592 44.2832 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 393.722592 90.637199 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 393.722592 136.991198 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 64.609196 181.838693 \nL 393.722592 181.838693 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 64.609196 184.851703 \nL 393.722592 184.851703 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 73.879996 187.980598 \nL 78.515396 178.709798 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 135.994356 90.637199 \nL 135.994356 44.2832 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 182.348355 136.991198 \nL 182.348355 44.2832 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 219.663324 67.460199 \nL 237.741384 67.460199 \nL 237.741384 21.1062 \nL 219.663324 21.1062 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 219.663324 113.814199 \nL 237.741384 113.814199 \nL 237.741384 67.460199 \nL 219.663324 67.460199 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 219.663324 160.168198 \nL 237.741384 160.168198 \nL 237.741384 113.814199 \nL 219.663324 113.814199 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 276.562859 44.2832 \nL 276.562859 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_10\">\n    <path d=\"M 273.549849 44.2832 \nL 273.549849 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_11\">\n    <path d=\"M 322.916858 90.637199 \nL 322.916858 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 319.903848 90.637199 \nL 319.903848 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_13\">\n    <path d=\"M 369.270857 136.991198 \nL 369.270857 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_14\">\n    <path d=\"M 366.257847 136.991198 \nL 366.257847 172.799663 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 74.575306 59.348249 \nL 104.705406 59.348249 \nL 104.705406 29.21815 \nL 74.575306 29.21815 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 135.994356 48.802714 \nC 137.192945 48.802714 138.342605 48.32651 139.190135 47.478979 \nC 140.037666 46.631448 140.51387 45.481789 140.51387 44.2832 \nC 140.51387 43.08461 140.037666 41.934951 139.190135 41.08742 \nC 138.342605 40.239889 137.192945 39.763685 135.994356 39.763685 \nC 134.795766 39.763685 133.646107 40.239889 132.798576 41.08742 \nC 131.951045 41.934951 131.474841 43.08461 131.474841 44.2832 \nC 131.474841 45.481789 131.951045 46.631448 132.798576 47.478979 \nC 133.646107 48.32651 134.795766 48.802714 135.994356 48.802714 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 135.994356 101.182734 \nC 138.791064 101.182734 141.473603 100.07159 143.451175 98.094018 \nC 145.428746 96.116447 146.53989 93.433907 146.53989 90.637199 \nC 146.53989 87.84049 145.428746 85.157951 143.451175 83.18038 \nC 141.473603 81.202808 138.791064 80.091664 135.994356 80.091664 \nC 133.197647 80.091664 130.515108 81.202808 128.537536 83.18038 \nC 126.559965 85.157951 125.448821 87.84049 125.448821 90.637199 \nC 125.448821 93.433907 126.559965 96.116447 128.537536 98.094018 \nC 130.515108 100.07159 133.197647 101.182734 135.994356 101.182734 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 182.348355 48.802714 \nC 183.546944 48.802714 184.696604 48.32651 185.544135 47.478979 \nC 186.391665 46.631448 186.86787 45.481789 186.86787 44.2832 \nC 186.86787 43.08461 186.391665 41.934951 185.544135 41.08742 \nC 184.696604 40.239889 183.546944 39.763685 182.348355 39.763685 \nC 181.149766 39.763685 180.000106 40.239889 179.152575 41.08742 \nC 178.305045 41.934951 177.82884 43.08461 177.82884 44.2832 \nC 177.82884 45.481789 178.305045 46.631448 179.152575 47.478979 \nC 180.000106 48.32651 181.149766 48.802714 182.348355 48.802714 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 182.348355 147.536733 \nC 185.145063 147.536733 187.827603 146.425589 189.805174 144.448017 \nC 191.782746 142.470446 192.89389 139.787907 192.89389 136.991198 \nC 192.89389 134.19449 191.782746 131.511951 189.805174 129.534379 \nC 187.827603 127.556807 185.145063 126.445663 182.348355 126.445663 \nC 179.551646 126.445663 176.869107 127.556807 174.891536 129.534379 \nC 172.913964 131.511951 171.80282 134.19449 171.80282 136.991198 \nC 171.80282 139.787907 172.913964 142.470446 174.891536 144.448017 \nC 176.869107 146.425589 179.551646 147.536733 182.348355 147.536733 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 259.991304 59.348249 \nL 290.121403 59.348249 \nL 290.121403 29.21815 \nL 259.991304 29.21815 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"stroke: #000000; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 285.601889 48.802714 \nC 285.601889 46.006937 284.490086 43.322808 282.513173 41.345895 \nC 280.53626 39.368982 277.852131 38.25718 275.056354 38.25718 \nC 272.260577 38.25718 269.576447 39.368982 267.599534 41.345895 \nC 265.622622 43.322808 264.510819 46.006937 264.510819 48.802714 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"line2d_15\">\n    <path d=\"M 275.056354 48.802714 \nL 285.601889 38.25718 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 306.345303 105.702249 \nL 336.475403 105.702249 \nL 336.475403 75.572149 \nL 306.345303 75.572149 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"stroke: #000000; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 331.955888 95.156714 \nC 331.955888 92.360937 330.844085 89.676808 328.867172 87.699895 \nC 326.890259 85.722982 324.20613 84.611179 321.410353 84.611179 \nC 318.614576 84.611179 315.930447 85.722982 313.953534 87.699895 \nC 311.976621 89.676808 310.864818 92.360937 310.864818 95.156714 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"line2d_16\">\n    <path d=\"M 321.410353 95.156714 \nL 331.955888 84.611179 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 352.699303 152.056248 \nL 382.829402 152.056248 \nL 382.829402 121.926148 \nL 352.699303 121.926148 \nz\n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"stroke: #000000; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 378.309887 141.510713 \nC 378.309887 138.714936 377.198085 136.030807 375.221172 134.053894 \nC 373.244259 132.076981 370.56013 130.965178 367.764352 130.965178 \nC 364.968575 130.965178 362.284446 132.076981 360.307533 134.053894 \nC 358.33062 136.030807 357.218818 138.714936 357.218818 141.510713 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 367.764352 141.510713 \nL 378.309887 130.965178 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- 3 -->\n     <g transform=\"translate(69.244596 176.546923) scale(0.104 -0.104)\">\n      <defs>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-33\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- c -->\n     <g transform=\"translate(46.403436 187.829182) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_6\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 47.870387) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-48\" d=\"M 628 4666 \nL 1259 4666 \nL 1259 2753 \nL 3553 2753 \nL 3553 4666 \nL 4184 4666 \nL 4184 0 \nL 3553 0 \nL 3553 2222 \nL 1259 2222 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 135.994356 96.663219 \nL 135.994356 84.611179 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 129.968336 90.637199 \nL 142.020375 90.637199 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 182.348355 143.017218 \nL 182.348355 130.965178 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 176.322335 136.991198 \nL 188.374375 136.991198 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_22\">\n    <path d=\"M 228.702354 21.1062 \nL 228.702354 67.460199 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_23\">\n    <path d=\"M 228.702354 67.460199 \nL 228.702354 113.814199 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_24\">\n    <path d=\"M 228.702354 113.814199 \nL 228.702354 160.168198 \n\" clip-path=\"url(#p04bcbc0ad6)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"text_7\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- 0 -->\n     <g transform=\"translate(286.644854 176.546923) scale(0.104 -0.104)\">\n      <use xlink:href=\"#DejaVuSans-30\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_8\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- 1 -->\n     <g transform=\"translate(332.998853 176.546923) scale(0.104 -0.104)\">\n      <use xlink:href=\"#DejaVuSans-31\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_9\">\n    <g clip-path=\"url(#p04bcbc0ad6)\">\n     <!-- 2 -->\n     <g transform=\"translate(379.352852 176.546923) scale(0.104 -0.104)\">\n      <use xlink:href=\"#DejaVuSans-32\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p04bcbc0ad6\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"391.157992\" height=\"203.957597\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 705.552x367.889 with 1 Axes>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir.barrier()\n",
    "print(cir.measure())\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以设定采样次数、进行部分测量以及显示理想的概率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'00': (62, tensor(0.5000)), '11': (38, tensor(0.5000))}\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"359.203993pt\" height=\"218.357597pt\" viewBox=\"0 0 359.203993 218.357597\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:25.249261</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 218.357597 \nL 359.203993 218.357597 \nL 359.203993 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 269.030334 172.799663 \nL 281.082374 172.799663 \nL 275.056354 181.491038 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #778899\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 315.384333 172.799663 \nL 327.436373 172.799663 \nL 321.410353 181.491038 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #778899\"/>\n   </g>\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 347.368593 44.2832 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 347.368593 90.637199 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 347.368593 136.991198 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 64.609196 181.838693 \nL 347.368593 181.838693 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 64.609196 184.851703 \nL 347.368593 184.851703 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 73.879996 187.980598 \nL 78.515396 178.709798 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 135.994356 90.637199 \nL 135.994356 44.2832 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 182.348355 136.991198 \nL 182.348355 44.2832 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 219.663324 67.460199 \nL 237.741384 67.460199 \nL 237.741384 21.1062 \nL 219.663324 21.1062 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 219.663324 113.814199 \nL 237.741384 113.814199 \nL 237.741384 67.460199 \nL 219.663324 67.460199 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 219.663324 160.168198 \nL 237.741384 160.168198 \nL 237.741384 113.814199 \nL 219.663324 113.814199 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 276.562859 90.637199 \nL 276.562859 172.799663 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_10\">\n    <path d=\"M 273.549849 90.637199 \nL 273.549849 172.799663 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_11\">\n    <path d=\"M 322.916858 136.991198 \nL 322.916858 172.799663 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 319.903848 136.991198 \nL 319.903848 172.799663 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #778899; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 74.575306 59.348249 \nL 104.705406 59.348249 \nL 104.705406 29.21815 \nL 74.575306 29.21815 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 135.994356 48.802714 \nC 137.192945 48.802714 138.342605 48.32651 139.190135 47.478979 \nC 140.037666 46.631448 140.51387 45.481789 140.51387 44.2832 \nC 140.51387 43.08461 140.037666 41.934951 139.190135 41.08742 \nC 138.342605 40.239889 137.192945 39.763685 135.994356 39.763685 \nC 134.795766 39.763685 133.646107 40.239889 132.798576 41.08742 \nC 131.951045 41.934951 131.474841 43.08461 131.474841 44.2832 \nC 131.474841 45.481789 131.951045 46.631448 132.798576 47.478979 \nC 133.646107 48.32651 134.795766 48.802714 135.994356 48.802714 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 135.994356 101.182734 \nC 138.791064 101.182734 141.473603 100.07159 143.451175 98.094018 \nC 145.428746 96.116447 146.53989 93.433907 146.53989 90.637199 \nC 146.53989 87.84049 145.428746 85.157951 143.451175 83.18038 \nC 141.473603 81.202808 138.791064 80.091664 135.994356 80.091664 \nC 133.197647 80.091664 130.515108 81.202808 128.537536 83.18038 \nC 126.559965 85.157951 125.448821 87.84049 125.448821 90.637199 \nC 125.448821 93.433907 126.559965 96.116447 128.537536 98.094018 \nC 130.515108 100.07159 133.197647 101.182734 135.994356 101.182734 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 182.348355 48.802714 \nC 183.546944 48.802714 184.696604 48.32651 185.544135 47.478979 \nC 186.391665 46.631448 186.86787 45.481789 186.86787 44.2832 \nC 186.86787 43.08461 186.391665 41.934951 185.544135 41.08742 \nC 184.696604 40.239889 183.546944 39.763685 182.348355 39.763685 \nC 181.149766 39.763685 180.000106 40.239889 179.152575 41.08742 \nC 178.305045 41.934951 177.82884 43.08461 177.82884 44.2832 \nC 177.82884 45.481789 178.305045 46.631448 179.152575 47.478979 \nC 180.000106 48.32651 181.149766 48.802714 182.348355 48.802714 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 182.348355 147.536733 \nC 185.145063 147.536733 187.827603 146.425589 189.805174 144.448017 \nC 191.782746 142.470446 192.89389 139.787907 192.89389 136.991198 \nC 192.89389 134.19449 191.782746 131.511951 189.805174 129.534379 \nC 187.827603 127.556807 185.145063 126.445663 182.348355 126.445663 \nC 179.551646 126.445663 176.869107 127.556807 174.891536 129.534379 \nC 172.913964 131.511951 171.80282 134.19449 171.80282 136.991198 \nC 171.80282 139.787907 172.913964 142.470446 174.891536 144.448017 \nC 176.869107 146.425589 179.551646 147.536733 182.348355 147.536733 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 259.991304 105.702249 \nL 290.121403 105.702249 \nL 290.121403 75.572149 \nL 259.991304 75.572149 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"stroke: #000000; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 285.601889 95.156714 \nC 285.601889 92.360937 284.490086 89.676808 282.513173 87.699895 \nC 280.53626 85.722982 277.852131 84.611179 275.056354 84.611179 \nC 272.260577 84.611179 269.576447 85.722982 267.599534 87.699895 \nC 265.622622 89.676808 264.510819 92.360937 264.510819 95.156714 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"line2d_13\">\n    <path d=\"M 275.056354 95.156714 \nL 285.601889 84.611179 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 306.345303 152.056248 \nL 336.475403 152.056248 \nL 336.475403 121.926148 \nL 306.345303 121.926148 \nz\n\" clip-path=\"url(#pdc7772fe46)\" style=\"stroke: #000000; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 331.955888 141.510713 \nC 331.955888 138.714936 330.844085 136.030807 328.867172 134.053894 \nC 326.890259 132.076981 324.20613 130.965178 321.410353 130.965178 \nC 318.614576 130.965178 315.930447 132.076981 313.953534 134.053894 \nC 311.976621 136.030807 310.864818 138.714936 310.864818 141.510713 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"line2d_14\">\n    <path d=\"M 321.410353 141.510713 \nL 331.955888 130.965178 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- 3 -->\n     <g transform=\"translate(69.244596 176.546923) scale(0.104 -0.104)\">\n      <defs>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-33\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- c -->\n     <g transform=\"translate(46.403436 187.829182) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-63\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_6\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 47.870387) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-48\" d=\"M 628 4666 \nL 1259 4666 \nL 1259 2753 \nL 3553 2753 \nL 3553 4666 \nL 4184 4666 \nL 4184 0 \nL 3553 0 \nL 3553 2222 \nL 1259 2222 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_15\">\n    <path d=\"M 135.994356 96.663219 \nL 135.994356 84.611179 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_16\">\n    <path d=\"M 129.968336 90.637199 \nL 142.020375 90.637199 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 182.348355 143.017218 \nL 182.348355 130.965178 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 176.322335 136.991198 \nL 188.374375 136.991198 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 228.702354 21.1062 \nL 228.702354 67.460199 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 228.702354 67.460199 \nL 228.702354 113.814199 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 228.702354 113.814199 \nL 228.702354 160.168198 \n\" clip-path=\"url(#pdc7772fe46)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"text_7\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- 1 -->\n     <g transform=\"translate(286.644854 176.546923) scale(0.104 -0.104)\">\n      <use xlink:href=\"#DejaVuSans-31\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_8\">\n    <g clip-path=\"url(#pdc7772fe46)\">\n     <!-- 2 -->\n     <g transform=\"translate(332.998853 176.546923) scale(0.104 -0.104)\">\n      <use xlink:href=\"#DejaVuSans-32\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pdc7772fe46\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"344.803993\" height=\"203.957597\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 621.941x367.889 with 1 Axes>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(cir.measure(shots=100, wires=[1,2], with_prob=True))\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "再来看一个对CNOT门实现分解的例子：$\\text{CNOT}=e^{-i{\\frac {\\pi }{4}}}R_{y_{1}}(-\\pi /2)R_{x_{1}}(-\\pi /2)R_{x_{2}}(-\\pi /2)R_{xx}(\\pi /2)R_{y_{1}}(\\pi /2)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.0000e+00+0.j,  0.0000e+00+0.j,  0.0000e+00+0.j, -1.7458e-08+0.j],\n",
      "        [ 0.0000e+00+0.j,  1.0000e+00+0.j, -1.7458e-08+0.j,  0.0000e+00+0.j],\n",
      "        [-1.7458e-08+0.j,  0.0000e+00+0.j,  0.0000e+00+0.j,  1.0000e+00+0.j],\n",
      "        [ 0.0000e+00+0.j, -1.7458e-08+0.j,  1.0000e+00+0.j,  0.0000e+00+0.j]])\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"266.384049pt\" height=\"125.649599pt\" viewBox=\"0 0 266.384049 125.649599\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:25.489161</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 125.649599 \nL 266.384049 125.649599 \nL 266.384049 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.497252 44.2832 \nL 254.548649 44.2832 \n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.497252 90.637199 \nL 254.548649 90.637199 \n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 74.463361 59.348249 \nL 104.593461 59.348249 \nL 104.593461 29.21815 \nL 74.463361 29.21815 \nz\n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 116.629277 105.702249 \nL 155.135544 105.702249 \nL 155.135544 29.21815 \nL 116.629277 29.21815 \nz\n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 167.17136 59.348249 \nL 197.30146 59.348249 \nL 197.30146 29.21815 \nL 167.17136 29.21815 \nz\n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 167.17136 105.702249 \nL 197.30146 105.702249 \nL 197.30146 75.572149 \nL 167.17136 75.572149 \nz\n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 213.52536 59.348249 \nL 243.655459 59.348249 \nL 243.655459 29.21815 \nL 213.52536 29.21815 \nz\n\" clip-path=\"url(#p1760c3d549)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.188952 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.188952 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- 1.57 -->\n     <g transform=\"translate(80.622161 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-31\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $\\mathrm{R_Y}$ -->\n     <g transform=\"translate(81.988411 43.350872) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-52\" d=\"M 2841 2188 \nQ 3044 2119 3236 1894 \nQ 3428 1669 3622 1275 \nL 4263 0 \nL 3584 0 \nL 2988 1197 \nQ 2756 1666 2539 1819 \nQ 2322 1972 1947 1972 \nL 1259 1972 \nL 1259 0 \nL 628 0 \nL 628 4666 \nL 2053 4666 \nQ 2853 4666 3247 4331 \nQ 3641 3997 3641 3322 \nQ 3641 2881 3436 2590 \nQ 3231 2300 2841 2188 \nz\nM 1259 4147 \nL 1259 2491 \nL 2053 2491 \nQ 2509 2491 2742 2702 \nQ 2975 2913 2975 3322 \nQ 2975 3731 2742 3939 \nQ 2509 4147 2053 4147 \nL 1259 4147 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-59\" d=\"M -13 4666 \nL 666 4666 \nL 1959 2747 \nL 3244 4666 \nL 3922 4666 \nL 2272 2222 \nL 2272 0 \nL 1638 0 \nL 1638 2222 \nL -13 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-59\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- 0 -->\n     <g transform=\"translate(119.874057 47.870387) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-30\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_6\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- 1 -->\n     <g transform=\"translate(119.874057 94.224386) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-31\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_7\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- 1.57 -->\n     <g transform=\"translate(132.075101 77.547879) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-31\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_8\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $\\mathrm{R_{XX}}$ -->\n     <g transform=\"translate(129.931351 63.630747) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-58\" d=\"M 403 4666 \nL 1081 4666 \nL 2241 2931 \nL 3406 4666 \nL 4084 4666 \nL 2584 2425 \nL 4184 0 \nL 3506 0 \nL 2194 1984 \nL 872 0 \nL 191 0 \nL 1856 2491 \nL 403 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(118.393555 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_9\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $-$1.57 -->\n     <g transform=\"translate(170.23641 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-2212\" d=\"M 678 2272 \nL 4684 2272 \nL 4684 1741 \nL 678 1741 \nL 678 2272 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-2212\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(83.789062 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(147.412109 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(179.199219 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-37\" transform=\"translate(235.572266 0.09375)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_10\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $\\mathrm{R_X}$ -->\n     <g transform=\"translate(174.30641 43.350872) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_11\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $-$1.57 -->\n     <g transform=\"translate(170.23641 101.883729) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-2212\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(83.789062 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(147.412109 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(179.199219 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-37\" transform=\"translate(235.572266 0.09375)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_12\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $\\mathrm{R_X}$ -->\n     <g transform=\"translate(174.30641 89.704871) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_13\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $-$1.57 -->\n     <g transform=\"translate(216.590409 55.529729) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-2212\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(83.789062 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(147.412109 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(179.199219 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-37\" transform=\"translate(235.572266 0.09375)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_14\">\n    <g clip-path=\"url(#p1760c3d549)\">\n     <!-- $\\mathrm{R_Y}$ -->\n     <g transform=\"translate(221.050409 43.350872) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-59\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p1760c3d549\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"251.984049\" height=\"111.249599\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 454.517x200.667 with 1 Axes>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(2)\n",
    "cir.ry(0, torch.pi/2)\n",
    "cir.rxx([0,1], torch.pi/2)\n",
    "cir.rx(1, -torch.pi/2)\n",
    "cir.rx(0, -torch.pi/2)\n",
    "cir.ry(0, -torch.pi/2)\n",
    "print(np.exp(-1j * np.pi / 4) * cir.get_unitary())\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CNOT、Toffoli、Fredkin也有不同的API去添加。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"498.265991pt\" height=\"172.003598pt\" viewBox=\"0 0 498.265991 172.003598\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:25.934906</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 172.003598 \nL 498.265991 172.003598 \nL 498.265991 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 486.430591 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 486.430591 90.637199 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 486.430591 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 89.640356 90.637199 \nL 89.640356 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 135.994356 90.637199 \nL 135.994356 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 182.348355 90.637199 \nL 182.348355 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 228.702354 136.991198 \nL 228.702354 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 275.056354 136.991198 \nL 275.056354 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 321.410353 136.991198 \nL 321.410353 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_10\">\n    <path d=\"M 367.764352 136.991198 \nL 367.764352 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_11\">\n    <path d=\"M 367.764352 90.637199 \nL 367.764352 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 414.118352 136.991198 \nL 414.118352 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_13\">\n    <path d=\"M 414.118352 90.637199 \nL 414.118352 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_14\">\n    <path d=\"M 460.472351 136.991198 \nL 460.472351 44.2832 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_15\">\n    <path d=\"M 460.472351 90.637199 \nL 460.472351 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_16\">\n    <path d=\"M 361.738333 96.663219 \nL 373.790372 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 361.738333 84.611179 \nL 373.790372 96.663219 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 361.738333 143.017218 \nL 373.790372 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 361.738333 130.965178 \nL 373.790372 143.017218 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 408.092332 96.663219 \nL 420.144372 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 408.092332 84.611179 \nL 420.144372 96.663219 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_22\">\n    <path d=\"M 408.092332 143.017218 \nL 420.144372 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_23\">\n    <path d=\"M 408.092332 130.965178 \nL 420.144372 143.017218 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_24\">\n    <path d=\"M 454.446331 96.663219 \nL 466.498371 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_25\">\n    <path d=\"M 454.446331 84.611179 \nL 466.498371 96.663219 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_26\">\n    <path d=\"M 454.446331 143.017218 \nL 466.498371 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_27\">\n    <path d=\"M 454.446331 130.965178 \nL 466.498371 143.017218 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 89.640356 48.802714 \nC 90.838946 48.802714 91.988605 48.32651 92.836136 47.478979 \nC 93.683666 46.631448 94.159871 45.481789 94.159871 44.2832 \nC 94.159871 43.08461 93.683666 41.934951 92.836136 41.08742 \nC 91.988605 40.239889 90.838946 39.763685 89.640356 39.763685 \nC 88.441767 39.763685 87.292107 40.239889 86.444576 41.08742 \nC 85.597046 41.934951 85.120841 43.08461 85.120841 44.2832 \nC 85.120841 45.481789 85.597046 46.631448 86.444576 47.478979 \nC 87.292107 48.32651 88.441767 48.802714 89.640356 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 89.640356 101.182734 \nC 92.437065 101.182734 95.119604 100.07159 97.097175 98.094018 \nC 99.074747 96.116447 100.185891 93.433907 100.185891 90.637199 \nC 100.185891 87.84049 99.074747 85.157951 97.097175 83.18038 \nC 95.119604 81.202808 92.437065 80.091664 89.640356 80.091664 \nC 86.843648 80.091664 84.161108 81.202808 82.183537 83.18038 \nC 80.205965 85.157951 79.094821 87.84049 79.094821 90.637199 \nC 79.094821 93.433907 80.205965 96.116447 82.183537 98.094018 \nC 84.161108 100.07159 86.843648 101.182734 89.640356 101.182734 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 135.994356 48.802714 \nC 137.192945 48.802714 138.342605 48.32651 139.190135 47.478979 \nC 140.037666 46.631448 140.51387 45.481789 140.51387 44.2832 \nC 140.51387 43.08461 140.037666 41.934951 139.190135 41.08742 \nC 138.342605 40.239889 137.192945 39.763685 135.994356 39.763685 \nC 134.795766 39.763685 133.646107 40.239889 132.798576 41.08742 \nC 131.951045 41.934951 131.474841 43.08461 131.474841 44.2832 \nC 131.474841 45.481789 131.951045 46.631448 132.798576 47.478979 \nC 133.646107 48.32651 134.795766 48.802714 135.994356 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 135.994356 101.182734 \nC 138.791064 101.182734 141.473603 100.07159 143.451175 98.094018 \nC 145.428746 96.116447 146.53989 93.433907 146.53989 90.637199 \nC 146.53989 87.84049 145.428746 85.157951 143.451175 83.18038 \nC 141.473603 81.202808 138.791064 80.091664 135.994356 80.091664 \nC 133.197647 80.091664 130.515108 81.202808 128.537536 83.18038 \nC 126.559965 85.157951 125.448821 87.84049 125.448821 90.637199 \nC 125.448821 93.433907 126.559965 96.116447 128.537536 98.094018 \nC 130.515108 100.07159 133.197647 101.182734 135.994356 101.182734 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 182.348355 48.802714 \nC 183.546944 48.802714 184.696604 48.32651 185.544135 47.478979 \nC 186.391665 46.631448 186.86787 45.481789 186.86787 44.2832 \nC 186.86787 43.08461 186.391665 41.934951 185.544135 41.08742 \nC 184.696604 40.239889 183.546944 39.763685 182.348355 39.763685 \nC 181.149766 39.763685 180.000106 40.239889 179.152575 41.08742 \nC 178.305045 41.934951 177.82884 43.08461 177.82884 44.2832 \nC 177.82884 45.481789 178.305045 46.631448 179.152575 47.478979 \nC 180.000106 48.32651 181.149766 48.802714 182.348355 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 182.348355 101.182734 \nC 185.145063 101.182734 187.827603 100.07159 189.805174 98.094018 \nC 191.782746 96.116447 192.89389 93.433907 192.89389 90.637199 \nC 192.89389 87.84049 191.782746 85.157951 189.805174 83.18038 \nC 187.827603 81.202808 185.145063 80.091664 182.348355 80.091664 \nC 179.551646 80.091664 176.869107 81.202808 174.891536 83.18038 \nC 172.913964 85.157951 171.80282 87.84049 171.80282 90.637199 \nC 171.80282 93.433907 172.913964 96.116447 174.891536 98.094018 \nC 176.869107 100.07159 179.551646 101.182734 182.348355 101.182734 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 228.702354 48.802714 \nC 229.900944 48.802714 231.050603 48.32651 231.898134 47.478979 \nC 232.745665 46.631448 233.221869 45.481789 233.221869 44.2832 \nC 233.221869 43.08461 232.745665 41.934951 231.898134 41.08742 \nC 231.050603 40.239889 229.900944 39.763685 228.702354 39.763685 \nC 227.503765 39.763685 226.354105 40.239889 225.506575 41.08742 \nC 224.659044 41.934951 224.182839 43.08461 224.182839 44.2832 \nC 224.182839 45.481789 224.659044 46.631448 225.506575 47.478979 \nC 226.354105 48.32651 227.503765 48.802714 228.702354 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 228.702354 95.156714 \nC 229.900944 95.156714 231.050603 94.680509 231.898134 93.832979 \nC 232.745665 92.985448 233.221869 91.835788 233.221869 90.637199 \nC 233.221869 89.43861 232.745665 88.28895 231.898134 87.441419 \nC 231.050603 86.593889 229.900944 86.117684 228.702354 86.117684 \nC 227.503765 86.117684 226.354105 86.593889 225.506575 87.441419 \nC 224.659044 88.28895 224.182839 89.43861 224.182839 90.637199 \nC 224.182839 91.835788 224.659044 92.985448 225.506575 93.832979 \nC 226.354105 94.680509 227.503765 95.156714 228.702354 95.156714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 228.702354 147.536733 \nC 231.499063 147.536733 234.181602 146.425589 236.159173 144.448017 \nC 238.136745 142.470446 239.247889 139.787907 239.247889 136.991198 \nC 239.247889 134.19449 238.136745 131.511951 236.159173 129.534379 \nC 234.181602 127.556807 231.499063 126.445663 228.702354 126.445663 \nC 225.905646 126.445663 223.223107 127.556807 221.245535 129.534379 \nC 219.267964 131.511951 218.156819 134.19449 218.156819 136.991198 \nC 218.156819 139.787907 219.267964 142.470446 221.245535 144.448017 \nC 223.223107 146.425589 225.905646 147.536733 228.702354 147.536733 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 275.056354 48.802714 \nC 276.254943 48.802714 277.404603 48.32651 278.252133 47.478979 \nC 279.099664 46.631448 279.575869 45.481789 279.575869 44.2832 \nC 279.575869 43.08461 279.099664 41.934951 278.252133 41.08742 \nC 277.404603 40.239889 276.254943 39.763685 275.056354 39.763685 \nC 273.857764 39.763685 272.708105 40.239889 271.860574 41.08742 \nC 271.013043 41.934951 270.536839 43.08461 270.536839 44.2832 \nC 270.536839 45.481789 271.013043 46.631448 271.860574 47.478979 \nC 272.708105 48.32651 273.857764 48.802714 275.056354 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 275.056354 95.156714 \nC 276.254943 95.156714 277.404603 94.680509 278.252133 93.832979 \nC 279.099664 92.985448 279.575869 91.835788 279.575869 90.637199 \nC 279.575869 89.43861 279.099664 88.28895 278.252133 87.441419 \nC 277.404603 86.593889 276.254943 86.117684 275.056354 86.117684 \nC 273.857764 86.117684 272.708105 86.593889 271.860574 87.441419 \nC 271.013043 88.28895 270.536839 89.43861 270.536839 90.637199 \nC 270.536839 91.835788 271.013043 92.985448 271.860574 93.832979 \nC 272.708105 94.680509 273.857764 95.156714 275.056354 95.156714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 275.056354 147.536733 \nC 277.853062 147.536733 280.535601 146.425589 282.513173 144.448017 \nC 284.490744 142.470446 285.601889 139.787907 285.601889 136.991198 \nC 285.601889 134.19449 284.490744 131.511951 282.513173 129.534379 \nC 280.535601 127.556807 277.853062 126.445663 275.056354 126.445663 \nC 272.259645 126.445663 269.577106 127.556807 267.599534 129.534379 \nC 265.621963 131.511951 264.510819 134.19449 264.510819 136.991198 \nC 264.510819 139.787907 265.621963 142.470446 267.599534 144.448017 \nC 269.577106 146.425589 272.259645 147.536733 275.056354 147.536733 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 321.410353 48.802714 \nC 322.608942 48.802714 323.758602 48.32651 324.606133 47.478979 \nC 325.453663 46.631448 325.929868 45.481789 325.929868 44.2832 \nC 325.929868 43.08461 325.453663 41.934951 324.606133 41.08742 \nC 323.758602 40.239889 322.608942 39.763685 321.410353 39.763685 \nC 320.211764 39.763685 319.062104 40.239889 318.214573 41.08742 \nC 317.367043 41.934951 316.890838 43.08461 316.890838 44.2832 \nC 316.890838 45.481789 317.367043 46.631448 318.214573 47.478979 \nC 319.062104 48.32651 320.211764 48.802714 321.410353 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 321.410353 95.156714 \nC 322.608942 95.156714 323.758602 94.680509 324.606133 93.832979 \nC 325.453663 92.985448 325.929868 91.835788 325.929868 90.637199 \nC 325.929868 89.43861 325.453663 88.28895 324.606133 87.441419 \nC 323.758602 86.593889 322.608942 86.117684 321.410353 86.117684 \nC 320.211764 86.117684 319.062104 86.593889 318.214573 87.441419 \nC 317.367043 88.28895 316.890838 89.43861 316.890838 90.637199 \nC 316.890838 91.835788 317.367043 92.985448 318.214573 93.832979 \nC 319.062104 94.680509 320.211764 95.156714 321.410353 95.156714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 321.410353 147.536733 \nC 324.207062 147.536733 326.889601 146.425589 328.867172 144.448017 \nC 330.844744 142.470446 331.955888 139.787907 331.955888 136.991198 \nC 331.955888 134.19449 330.844744 131.511951 328.867172 129.534379 \nC 326.889601 127.556807 324.207062 126.445663 321.410353 126.445663 \nC 318.613645 126.445663 315.931105 127.556807 313.953534 129.534379 \nC 311.975962 131.511951 310.864818 134.19449 310.864818 136.991198 \nC 310.864818 139.787907 311.975962 142.470446 313.953534 144.448017 \nC 315.931105 146.425589 318.613645 147.536733 321.410353 147.536733 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 367.764352 48.802714 \nC 368.962942 48.802714 370.112601 48.32651 370.960132 47.478979 \nC 371.807663 46.631448 372.283867 45.481789 372.283867 44.2832 \nC 372.283867 43.08461 371.807663 41.934951 370.960132 41.08742 \nC 370.112601 40.239889 368.962942 39.763685 367.764352 39.763685 \nC 366.565763 39.763685 365.416103 40.239889 364.568573 41.08742 \nC 363.721042 41.934951 363.244837 43.08461 363.244837 44.2832 \nC 363.244837 45.481789 363.721042 46.631448 364.568573 47.478979 \nC 365.416103 48.32651 366.565763 48.802714 367.764352 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 414.118352 48.802714 \nC 415.316941 48.802714 416.466601 48.32651 417.314131 47.478979 \nC 418.161662 46.631448 418.637867 45.481789 418.637867 44.2832 \nC 418.637867 43.08461 418.161662 41.934951 417.314131 41.08742 \nC 416.466601 40.239889 415.316941 39.763685 414.118352 39.763685 \nC 412.919762 39.763685 411.770103 40.239889 410.922572 41.08742 \nC 410.075041 41.934951 409.598837 43.08461 409.598837 44.2832 \nC 409.598837 45.481789 410.075041 46.631448 410.922572 47.478979 \nC 411.770103 48.32651 412.919762 48.802714 414.118352 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_19\">\n    <path d=\"M 460.472351 48.802714 \nC 461.670941 48.802714 462.8206 48.32651 463.668131 47.478979 \nC 464.515662 46.631448 464.991866 45.481789 464.991866 44.2832 \nC 464.991866 43.08461 464.515662 41.934951 463.668131 41.08742 \nC 462.8206 40.239889 461.670941 39.763685 460.472351 39.763685 \nC 459.273762 39.763685 458.124102 40.239889 457.276572 41.08742 \nC 456.429041 41.934951 455.952836 43.08461 455.952836 44.2832 \nC 455.952836 45.481789 456.429041 46.631448 457.276572 47.478979 \nC 458.124102 48.32651 459.273762 48.802714 460.472351 48.802714 \nz\n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#pa1fb85fa2a)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#pa1fb85fa2a)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#pa1fb85fa2a)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_28\">\n    <path d=\"M 89.640356 96.663219 \nL 89.640356 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_29\">\n    <path d=\"M 83.614336 90.637199 \nL 95.666376 90.637199 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_30\">\n    <path d=\"M 135.994356 96.663219 \nL 135.994356 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_31\">\n    <path d=\"M 129.968336 90.637199 \nL 142.020375 90.637199 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_32\">\n    <path d=\"M 182.348355 96.663219 \nL 182.348355 84.611179 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_33\">\n    <path d=\"M 176.322335 90.637199 \nL 188.374375 90.637199 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_34\">\n    <path d=\"M 228.702354 143.017218 \nL 228.702354 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_35\">\n    <path d=\"M 222.676334 136.991198 \nL 234.728374 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_36\">\n    <path d=\"M 275.056354 143.017218 \nL 275.056354 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_37\">\n    <path d=\"M 269.030334 136.991198 \nL 281.082374 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_38\">\n    <path d=\"M 321.410353 143.017218 \nL 321.410353 130.965178 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_39\">\n    <path d=\"M 315.384333 136.991198 \nL 327.436373 136.991198 \n\" clip-path=\"url(#pa1fb85fa2a)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pa1fb85fa2a\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"483.865991\" height=\"157.603598\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 872.774x284.278 with 1 Axes>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(3)\n",
    "cir.cnot(0, 1)\n",
    "cir.cx(0, 1)\n",
    "cir.x(1, 0)\n",
    "\n",
    "cir.toffoli(0, 1, 2)\n",
    "cir.ccx(0, 1, 2)\n",
    "cir.x(2, [0,1])\n",
    "\n",
    "cir.fredkin(0, 1, 2)\n",
    "cir.cswap(0, 1, 2)\n",
    "cir.swap([1,2], 0)\n",
    "\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数化量子线路\n",
    "\n",
    "DeepQuantum可以帮助用户很方便地实现参数化量子线路，从而进行量子机器学习。\n",
    "\n",
    "`QubitCircuit`的实例中添加的带参数的量子门，如果没有指定输入参数，会自动初始化变分参数。\n",
    "\n",
    "补充说明：如果指定了输入参数，那么输入参数会在量子门中被记录为buffer，从而保留其原来的性质。比如，参数不需要求梯度时，就会保持不变。又比如，参数是上一层神经网络的输出，那么在backward过程中就会记录梯度，但它的更新不是通过`QubitCircuit`而是上一层神经网络本身。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"359.203993pt\" height=\"218.357597pt\" viewBox=\"0 0 359.203993 218.357597\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:26.185452</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 218.357597 \nL 359.203993 218.357597 \nL 359.203993 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 347.368593 44.2832 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 347.368593 90.637199 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 347.368593 136.991198 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 64.609196 183.345198 \nL 347.368593 183.345198 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 205.525355 136.991198 \nL 205.525355 90.637199 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 205.525355 136.991198 \nL 205.525355 90.637199 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 298.233353 183.345198 \nL 298.233353 44.2832 \n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: none; stroke: #bb8bff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 97.752306 59.348249 \nL 127.882406 59.348249 \nL 127.882406 29.21815 \nL 97.752306 29.21815 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 93.564222 152.056248 \nL 132.070489 152.056248 \nL 132.070489 75.572149 \nL 93.564222 75.572149 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 78.626646 198.410247 \nL 147.008066 198.410247 \nL 147.008066 168.280148 \nL 78.626646 168.280148 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 190.460305 59.348249 \nL 220.590404 59.348249 \nL 220.590404 29.21815 \nL 190.460305 29.21815 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 205.525355 95.156714 \nC 206.723944 95.156714 207.873604 94.680509 208.721134 93.832979 \nC 209.568665 92.985448 210.04487 91.835788 210.04487 90.637199 \nC 210.04487 89.43861 209.568665 88.28895 208.721134 87.441419 \nC 207.873604 86.593889 206.723944 86.117684 205.525355 86.117684 \nC 204.326765 86.117684 203.177106 86.593889 202.329575 87.441419 \nC 201.482044 88.28895 201.00584 89.43861 201.00584 90.637199 \nC 201.00584 91.835788 201.482044 92.985448 202.329575 93.832979 \nC 203.177106 94.680509 204.326765 95.156714 205.525355 95.156714 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 205.525355 141.510713 \nC 206.723944 141.510713 207.873604 141.034509 208.721134 140.186978 \nC 209.568665 139.339447 210.04487 138.189788 210.04487 136.991198 \nC 210.04487 135.792609 209.568665 134.642949 208.721134 133.795419 \nC 207.873604 132.947888 206.723944 132.471683 205.525355 132.471683 \nC 204.326765 132.471683 203.177106 132.947888 202.329575 133.795419 \nC 201.482044 134.642949 201.00584 135.792609 201.00584 136.991198 \nC 201.00584 138.189788 201.482044 139.339447 202.329575 140.186978 \nC 203.177106 141.034509 204.326765 141.510713 205.525355 141.510713 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 298.233353 187.864713 \nC 299.431943 187.864713 300.581602 187.388508 301.429133 186.540977 \nC 302.276664 185.693447 302.752868 184.543787 302.752868 183.345198 \nC 302.752868 182.146608 302.276664 180.996949 301.429133 180.149418 \nC 300.581602 179.301887 299.431943 178.825683 298.233353 178.825683 \nC 297.034764 178.825683 295.885104 179.301887 295.037574 180.149418 \nC 294.190043 180.996949 293.713838 182.146608 293.713838 183.345198 \nC 293.713838 184.543787 294.190043 185.693447 295.037574 186.540977 \nC 295.885104 187.388508 297.034764 187.864713 298.233353 187.864713 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 255.979365 59.348249 \nL 340.487342 59.348249 \nL 340.487342 29.21815 \nL 255.979365 29.21815 \nz\n\" clip-path=\"url(#p6cd32d91cb)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- ${q}_{3}$ -->\n     <g transform=\"translate(37.300897 187.796174) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 6.6 -->\n     <g transform=\"translate(106.456106 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-36\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"95.410156\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_6\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- $\\mathrm{R_X}$ -->\n     <g transform=\"translate(104.887356 43.350872) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-52\" d=\"M 2841 2188 \nQ 3044 2119 3236 1894 \nQ 3428 1669 3622 1275 \nL 4263 0 \nL 3584 0 \nL 2988 1197 \nQ 2756 1666 2539 1819 \nQ 2322 1972 1947 1972 \nL 1259 1972 \nL 1259 0 \nL 628 0 \nL 628 4666 \nL 2053 4666 \nQ 2853 4666 3247 4331 \nQ 3641 3997 3641 3322 \nQ 3641 2881 3436 2590 \nQ 3231 2300 2841 2188 \nz\nM 1259 4147 \nL 1259 2491 \nL 2053 2491 \nQ 2509 2491 2742 2702 \nQ 2975 2913 2975 3322 \nQ 2975 3731 2742 3939 \nQ 2509 4147 2053 4147 \nL 1259 4147 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-58\" d=\"M 403 4666 \nL 1081 4666 \nL 2241 2931 \nL 3406 4666 \nL 4084 4666 \nL 2584 2425 \nL 4184 0 \nL 3506 0 \nL 2194 1984 \nL 872 0 \nL 191 0 \nL 1856 2491 \nL 403 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_7\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 0 -->\n     <g transform=\"translate(96.809002 94.224386) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-30\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_8\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 1 -->\n     <g transform=\"translate(96.809002 140.578386) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-31\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_9\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 7.5 -->\n     <g transform=\"translate(111.555046 123.901878) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-37\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"95.410156\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_10\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- $\\mathrm{R_{XX}}$ -->\n     <g transform=\"translate(106.866296 109.984746) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(118.393555 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_11\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 2.16, 4.1, 0.729 -->\n     <g transform=\"translate(81.013606 194.591728) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-2c\" d=\"M 750 794 \nL 1409 794 \nL 1409 256 \nL 897 -744 \nL 494 -744 \nL 750 256 \nL 750 794 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-39\" d=\"M 703 97 \nL 703 672 \nQ 941 559 1184 500 \nQ 1428 441 1663 441 \nQ 2288 441 2617 861 \nQ 2947 1281 2994 2138 \nQ 2813 1869 2534 1725 \nQ 2256 1581 1919 1581 \nQ 1219 1581 811 2004 \nQ 403 2428 403 3163 \nQ 403 3881 828 4315 \nQ 1253 4750 1959 4750 \nQ 2769 4750 3195 4129 \nQ 3622 3509 3622 2328 \nQ 3622 1225 3098 567 \nQ 2575 -91 1691 -91 \nQ 1453 -91 1209 -44 \nQ 966 3 703 97 \nz\nM 1959 2075 \nQ 2384 2075 2632 2365 \nQ 2881 2656 2881 3163 \nQ 2881 3666 2632 3958 \nQ 2384 4250 1959 4250 \nQ 1534 4250 1286 3958 \nQ 1038 3666 1038 3163 \nQ 1038 2656 1286 2365 \nQ 1534 2075 1959 2075 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-32\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"254.443359\"/>\n      <use xlink:href=\"#DejaVuSans-34\" x=\"286.230469\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"381.640625\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"477.050781\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"508.837891\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"572.460938\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"604.248047\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"667.871094\"/>\n      <use xlink:href=\"#DejaVuSans-39\" x=\"731.494141\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_12\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- U -->\n     <g transform=\"translate(108.060168 182.41287) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-55\" d=\"M 556 4666 \nL 1191 4666 \nL 1191 1831 \nQ 1191 1081 1462 751 \nQ 1734 422 2344 422 \nQ 2950 422 3222 751 \nQ 3494 1081 3494 1831 \nL 3494 4666 \nL 4128 4666 \nL 4128 1753 \nQ 4128 841 3676 375 \nQ 3225 -91 2344 -91 \nQ 1459 -91 1007 375 \nQ 556 841 556 1753 \nL 556 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_13\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 5.78 -->\n     <g transform=\"translate(196.619105 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-35\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-38\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_14\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- P -->\n     <g transform=\"translate(201.606058 43.350872) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-50\" d=\"M 1259 4147 \nL 1259 2394 \nL 2053 2394 \nQ 2494 2394 2734 2622 \nQ 2975 2850 2975 3272 \nQ 2975 3691 2734 3919 \nQ 2494 4147 2053 4147 \nL 1259 4147 \nz\nM 628 4666 \nL 2053 4666 \nQ 2838 4666 3239 4311 \nQ 3641 3956 3641 3272 \nQ 3641 2581 3239 2228 \nQ 2838 1875 2053 1875 \nL 1259 1875 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-50\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_15\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- P (2.4) -->\n     <g transform=\"translate(211.703254 112.939849) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-28\" d=\"M 1984 4856 \nQ 1566 4138 1362 3434 \nQ 1159 2731 1159 2009 \nQ 1159 1288 1364 580 \nQ 1569 -128 1984 -844 \nL 1484 -844 \nQ 1016 -109 783 600 \nQ 550 1309 550 2009 \nQ 550 2706 781 3412 \nQ 1013 4119 1484 4856 \nL 1984 4856 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-29\" d=\"M 513 4856 \nL 1013 4856 \nQ 1481 4119 1714 3412 \nQ 1947 2706 1947 2009 \nQ 1947 1309 1714 600 \nQ 1481 -109 1013 -844 \nL 513 -844 \nQ 928 -128 1133 580 \nQ 1338 1288 1338 2009 \nQ 1338 2731 1133 3434 \nQ 928 4138 513 4856 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-50\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"60.302734\"/>\n      <use xlink:href=\"#DejaVuSans-28\" x=\"92.089844\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"131.103516\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"194.726562\"/>\n      <use xlink:href=\"#DejaVuSans-34\" x=\"226.513672\"/>\n      <use xlink:href=\"#DejaVuSans-29\" x=\"290.136719\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_16\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- 2.52, 0.466, 1.58, 0 -->\n     <g transform=\"translate(258.797103 55.529729) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-32\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"254.443359\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"286.230469\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-34\" x=\"381.640625\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"508.886719\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"572.509766\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"604.296875\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"636.083984\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"699.707031\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"731.494141\"/>\n      <use xlink:href=\"#DejaVuSans-38\" x=\"795.117188\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"858.740234\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"890.527344\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"922.314453\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_17\">\n    <g clip-path=\"url(#p6cd32d91cb)\">\n     <!-- U -->\n     <g transform=\"translate(293.476166 43.350872) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p6cd32d91cb\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"344.803993\" height=\"203.957597\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 621.941x367.889 with 1 Axes>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(4)\n",
    "cir.rx(0)\n",
    "cir.rxx([1,2])\n",
    "cir.u3(3)\n",
    "cir.p(0)\n",
    "cir.cu(3, 0)\n",
    "cir.cp(1, 2)\n",
    "cir.draw()\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以直接添加一层量子门，并通过`wires`指定放置于哪几条线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"312.849993pt\" height=\"218.357597pt\" viewBox=\"0 0 312.849993 218.357597\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:26.459088</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 218.357597 \nL 312.849993 218.357597 \nL 312.849993 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 301.014593 44.2832 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 301.014593 90.637199 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 301.014593 136.991198 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 64.609196 183.345198 \nL 301.014593 183.345198 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 275.056354 90.637199 \nL 275.056354 44.2832 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 275.056354 183.345198 \nL 275.056354 136.991198 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 74.575306 59.348249 \nL 104.705406 59.348249 \nL 104.705406 29.21815 \nL 74.575306 29.21815 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 74.575306 105.702249 \nL 104.705406 105.702249 \nL 104.705406 75.572149 \nL 74.575306 75.572149 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 74.575306 152.056248 \nL 104.705406 152.056248 \nL 104.705406 121.926148 \nL 74.575306 121.926148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 74.575306 198.410247 \nL 104.705406 198.410247 \nL 104.705406 168.280148 \nL 74.575306 168.280148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 120.929306 59.348249 \nL 151.059405 59.348249 \nL 151.059405 29.21815 \nL 120.929306 29.21815 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 120.929306 105.702249 \nL 151.059405 105.702249 \nL 151.059405 75.572149 \nL 120.929306 75.572149 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 120.929306 152.056248 \nL 151.059405 152.056248 \nL 151.059405 121.926148 \nL 120.929306 121.926148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 120.929306 198.410247 \nL 151.059405 198.410247 \nL 151.059405 168.280148 \nL 120.929306 168.280148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 171.334645 59.348249 \nL 239.716065 59.348249 \nL 239.716065 29.21815 \nL 171.334645 29.21815 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 171.383316 105.702249 \nL 239.667393 105.702249 \nL 239.667393 75.572149 \nL 171.383316 75.572149 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 168.581217 152.056248 \nL 242.469492 152.056248 \nL 242.469492 121.926148 \nL 168.581217 121.926148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 166.022476 198.410247 \nL 245.028233 198.410247 \nL 245.028233 168.280148 \nL 166.022476 168.280148 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #bb8bff; stroke: #bb8bff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 275.056354 48.802714 \nC 276.254943 48.802714 277.404603 48.32651 278.252133 47.478979 \nC 279.099664 46.631448 279.575869 45.481789 279.575869 44.2832 \nC 279.575869 43.08461 279.099664 41.934951 278.252133 41.08742 \nC 277.404603 40.239889 276.254943 39.763685 275.056354 39.763685 \nC 273.857764 39.763685 272.708105 40.239889 271.860574 41.08742 \nC 271.013043 41.934951 270.536839 43.08461 270.536839 44.2832 \nC 270.536839 45.481789 271.013043 46.631448 271.860574 47.478979 \nC 272.708105 48.32651 273.857764 48.802714 275.056354 48.802714 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 275.056354 101.182734 \nC 277.853062 101.182734 280.535601 100.07159 282.513173 98.094018 \nC 284.490744 96.116447 285.601889 93.433907 285.601889 90.637199 \nC 285.601889 87.84049 284.490744 85.157951 282.513173 83.18038 \nC 280.535601 81.202808 277.853062 80.091664 275.056354 80.091664 \nC 272.259645 80.091664 269.577106 81.202808 267.599534 83.18038 \nC 265.621963 85.157951 264.510819 87.84049 264.510819 90.637199 \nC 264.510819 93.433907 265.621963 96.116447 267.599534 98.094018 \nC 269.577106 100.07159 272.259645 101.182734 275.056354 101.182734 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 275.056354 141.510713 \nC 276.254943 141.510713 277.404603 141.034509 278.252133 140.186978 \nC 279.099664 139.339447 279.575869 138.189788 279.575869 136.991198 \nC 279.575869 135.792609 279.099664 134.642949 278.252133 133.795419 \nC 277.404603 132.947888 276.254943 132.471683 275.056354 132.471683 \nC 273.857764 132.471683 272.708105 132.947888 271.860574 133.795419 \nC 271.013043 134.642949 270.536839 135.792609 270.536839 136.991198 \nC 270.536839 138.189788 271.013043 139.339447 271.860574 140.186978 \nC 272.708105 141.034509 273.857764 141.510713 275.056354 141.510713 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 275.056354 193.890733 \nC 277.853062 193.890733 280.535601 192.779588 282.513173 190.802017 \nC 284.490744 188.824445 285.601889 186.141906 285.601889 183.345198 \nC 285.601889 180.548489 284.490744 177.86595 282.513173 175.888378 \nC 280.535601 173.910807 277.853062 172.799663 275.056354 172.799663 \nC 272.259645 172.799663 269.577106 173.910807 267.599534 175.888378 \nC 265.621963 177.86595 264.510819 180.548489 264.510819 183.345198 \nC 264.510819 186.141906 265.621963 188.824445 267.599534 190.802017 \nC 269.577106 192.779588 272.259645 193.890733 275.056354 193.890733 \nz\n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- ${q}_{3}$ -->\n     <g transform=\"translate(37.300897 187.796174) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 47.870387) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-48\" d=\"M 628 4666 \nL 1259 4666 \nL 1259 2753 \nL 3553 2753 \nL 3553 4666 \nL 4184 4666 \nL 4184 0 \nL 3553 0 \nL 3553 2222 \nL 1259 2222 \nL 1259 0 \nL 628 0 \nL 628 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_6\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 94.224386) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_7\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 140.578386) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_8\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- H -->\n     <g transform=\"translate(84.752153 186.932385) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-48\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_9\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 11.7 -->\n     <g transform=\"translate(127.088106 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-37\" d=\"M 525 4666 \nL 3525 4666 \nL 3525 4397 \nL 1831 0 \nL 1172 0 \nL 2766 4134 \nL 525 4134 \nL 525 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-31\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"127.246094\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_10\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- $\\mathrm{R_X}$ -->\n     <g transform=\"translate(128.064356 43.350872) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-52\" d=\"M 2841 2188 \nQ 3044 2119 3236 1894 \nQ 3428 1669 3622 1275 \nL 4263 0 \nL 3584 0 \nL 2988 1197 \nQ 2756 1666 2539 1819 \nQ 2322 1972 1947 1972 \nL 1259 1972 \nL 1259 0 \nL 628 0 \nL 628 4666 \nL 2053 4666 \nQ 2853 4666 3247 4331 \nQ 3641 3997 3641 3322 \nQ 3641 2881 3436 2590 \nQ 3231 2300 2841 2188 \nz\nM 1259 4147 \nL 1259 2491 \nL 2053 2491 \nQ 2509 2491 2742 2702 \nQ 2975 2913 2975 3322 \nQ 2975 3731 2742 3939 \nQ 2509 4147 2053 4147 \nL 1259 4147 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-58\" d=\"M 403 4666 \nL 1081 4666 \nL 2241 2931 \nL 3406 4666 \nL 4084 4666 \nL 2584 2425 \nL 4184 0 \nL 3506 0 \nL 2194 1984 \nL 872 0 \nL 191 0 \nL 1856 2491 \nL 403 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_11\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 2.16 -->\n     <g transform=\"translate(127.088106 101.883729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-32\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_12\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- $\\mathrm{R_Y}$ -->\n     <g transform=\"translate(128.454356 89.704871) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-59\" d=\"M -13 4666 \nL 666 4666 \nL 1959 2747 \nL 3244 4666 \nL 3922 4666 \nL 2272 2222 \nL 2272 0 \nL 1638 0 \nL 1638 2222 \nL -13 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-59\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_13\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 4.91 -->\n     <g transform=\"translate(127.088106 148.237728) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-39\" d=\"M 703 97 \nL 703 672 \nQ 941 559 1184 500 \nQ 1428 441 1663 441 \nQ 2288 441 2617 861 \nQ 2947 1281 2994 2138 \nQ 2813 1869 2534 1725 \nQ 2256 1581 1919 1581 \nQ 1219 1581 811 2004 \nQ 403 2428 403 3163 \nQ 403 3881 828 4315 \nQ 1253 4750 1959 4750 \nQ 2769 4750 3195 4129 \nQ 3622 3509 3622 2328 \nQ 3622 1225 3098 567 \nQ 2575 -91 1691 -91 \nQ 1453 -91 1209 -44 \nQ 966 3 703 97 \nz\nM 1959 2075 \nQ 2384 2075 2632 2365 \nQ 2881 2656 2881 3163 \nQ 2881 3666 2632 3958 \nQ 2384 4250 1959 4250 \nQ 1534 4250 1286 3958 \nQ 1038 3666 1038 3163 \nQ 1038 2656 1286 2365 \nQ 1534 2075 1959 2075 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-34\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-39\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_14\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- $\\mathrm{R_X}$ -->\n     <g transform=\"translate(128.064356 136.058871) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-58\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_15\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 4.73 -->\n     <g transform=\"translate(127.088106 194.591728) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-34\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"159.033203\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_16\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- $\\mathrm{R_Y}$ -->\n     <g transform=\"translate(128.454356 182.41287) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-52\" transform=\"translate(0 0.09375)\"/>\n      <use xlink:href=\"#DejaVuSans-59\" transform=\"translate(70.439453 -16.3125) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_17\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 2.98, 3.54, 1.18 -->\n     <g transform=\"translate(173.721605 55.529729) scale(0.08 -0.08)\">\n      <defs>\n       <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-2c\" d=\"M 750 794 \nL 1409 794 \nL 1409 256 \nL 897 -744 \nL 494 -744 \nL 750 256 \nL 750 794 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-32\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-39\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-38\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"254.443359\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"286.230469\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"381.640625\"/>\n      <use xlink:href=\"#DejaVuSans-34\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"508.886719\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"540.673828\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"572.460938\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"636.083984\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"667.871094\"/>\n      <use xlink:href=\"#DejaVuSans-38\" x=\"731.494141\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_18\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- U -->\n     <g transform=\"translate(200.768167 43.350872) scale(0.13 -0.13)\">\n      <defs>\n       <path id=\"DejaVuSans-55\" d=\"M 556 4666 \nL 1191 4666 \nL 1191 1831 \nQ 1191 1081 1462 751 \nQ 1734 422 2344 422 \nQ 2950 422 3222 751 \nQ 3494 1081 3494 1831 \nL 3494 4666 \nL 4128 4666 \nL 4128 1753 \nQ 4128 841 3676 375 \nQ 3225 -91 2344 -91 \nQ 1459 -91 1007 375 \nQ 556 841 556 1753 \nL 556 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_19\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 3.03, 2.62, 5.42 -->\n     <g transform=\"translate(173.721605 101.883729) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-33\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"254.443359\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"286.230469\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"381.640625\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"508.886719\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"540.673828\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"572.460938\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"636.083984\"/>\n      <use xlink:href=\"#DejaVuSans-34\" x=\"667.871094\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"731.494141\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_20\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- U -->\n     <g transform=\"translate(200.768167 89.704871) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_21\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 0.723, 1.87, 2.59 -->\n     <g transform=\"translate(171.176605 148.237728) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-30\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"286.279297\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"318.066406\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"413.476562\"/>\n      <use xlink:href=\"#DejaVuSans-38\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"508.886719\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"572.509766\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"604.296875\"/>\n      <use xlink:href=\"#DejaVuSans-32\" x=\"636.083984\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"699.707031\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"731.494141\"/>\n      <use xlink:href=\"#DejaVuSans-39\" x=\"795.117188\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_22\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- U -->\n     <g transform=\"translate(200.768167 136.058871) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_23\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- 0.937, 0.75, 0.316 -->\n     <g transform=\"translate(168.631605 194.591728) scale(0.08 -0.08)\">\n      <use xlink:href=\"#DejaVuSans-30\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"63.623047\"/>\n      <use xlink:href=\"#DejaVuSans-39\" x=\"95.410156\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"159.033203\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"222.65625\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"286.279297\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"318.066406\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"349.853516\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"413.476562\"/>\n      <use xlink:href=\"#DejaVuSans-37\" x=\"445.263672\"/>\n      <use xlink:href=\"#DejaVuSans-35\" x=\"508.886719\"/>\n      <use xlink:href=\"#DejaVuSans-2c\" x=\"572.509766\"/>\n      <use xlink:href=\"#DejaVuSans-20\" x=\"604.296875\"/>\n      <use xlink:href=\"#DejaVuSans-30\" x=\"636.083984\"/>\n      <use xlink:href=\"#DejaVuSans-2e\" x=\"699.707031\"/>\n      <use xlink:href=\"#DejaVuSans-33\" x=\"731.494141\"/>\n      <use xlink:href=\"#DejaVuSans-31\" x=\"795.117188\"/>\n      <use xlink:href=\"#DejaVuSans-36\" x=\"858.740234\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_24\">\n    <g clip-path=\"url(#p8bc502efa3)\">\n     <!-- U -->\n     <g transform=\"translate(200.768167 182.41287) scale(0.13 -0.13)\">\n      <use xlink:href=\"#DejaVuSans-55\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 275.056354 96.663219 \nL 275.056354 84.611179 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 269.030334 90.637199 \nL 281.082374 90.637199 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 275.056354 189.371218 \nL 275.056354 177.319178 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_10\">\n    <path d=\"M 269.030334 183.345198 \nL 281.082374 183.345198 \n\" clip-path=\"url(#p8bc502efa3)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p8bc502efa3\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"298.449993\" height=\"203.957597\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 538.33x367.889 with 1 Axes>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(4)\n",
    "cir.hlayer()\n",
    "cir.rxlayer([0,2])\n",
    "cir.rylayer([1,3])\n",
    "cir.u3layer()\n",
    "cir.cxlayer()\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`cnot_ring()`可以用`minmax`参数指定线路范围，`step`设定每一对control和target相隔的距离，`reverse`指定是否从大到小。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"544.61999pt\" height=\"264.711597pt\" viewBox=\"0 0 544.61999 264.711597\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2023-04-17T18:04:26.869870</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.7.1, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 264.711597 \nL 544.61999 264.711597 \nL 544.61999 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"line2d_1\">\n    <path d=\"M 64.609196 44.2832 \nL 532.78459 44.2832 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_2\">\n    <path d=\"M 64.609196 90.637199 \nL 532.78459 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_3\">\n    <path d=\"M 64.609196 136.991198 \nL 532.78459 136.991198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_4\">\n    <path d=\"M 64.609196 183.345198 \nL 532.78459 183.345198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_5\">\n    <path d=\"M 64.609196 229.699197 \nL 532.78459 229.699197 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #000000; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_6\">\n    <path d=\"M 89.640356 90.637199 \nL 89.640356 44.2832 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_7\">\n    <path d=\"M 135.994356 136.991198 \nL 135.994356 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_8\">\n    <path d=\"M 182.348355 183.345198 \nL 182.348355 136.991198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_9\">\n    <path d=\"M 228.702354 229.699197 \nL 228.702354 183.345198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_10\">\n    <path d=\"M 275.056354 229.699197 \nL 275.056354 44.2832 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_2\">\n    <path d=\"M 312.371323 67.460199 \nL 330.449383 67.460199 \nL 330.449383 21.1062 \nL 312.371323 21.1062 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 312.371323 113.814199 \nL 330.449383 113.814199 \nL 330.449383 67.460199 \nL 312.371323 67.460199 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 312.371323 160.168198 \nL 330.449383 160.168198 \nL 330.449383 113.814199 \nL 312.371323 113.814199 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 312.371323 206.522197 \nL 330.449383 206.522197 \nL 330.449383 160.168198 \nL 312.371323 160.168198 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 312.371323 252.876197 \nL 330.449383 252.876197 \nL 330.449383 206.522197 \nL 312.371323 206.522197 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #bdbdbd; opacity: 0.6\"/>\n   </g>\n   <g id=\"line2d_11\">\n    <path d=\"M 367.764352 229.699197 \nL 367.764352 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_12\">\n    <path d=\"M 414.118352 229.699197 \nL 414.118352 183.345198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_13\">\n    <path d=\"M 460.472351 183.345198 \nL 460.472351 136.991198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_14\">\n    <path d=\"M 506.826351 136.991198 \nL 506.826351 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #6fa4ff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_7\">\n    <path d=\"M 89.640356 48.802714 \nC 90.838946 48.802714 91.988605 48.32651 92.836136 47.478979 \nC 93.683666 46.631448 94.159871 45.481789 94.159871 44.2832 \nC 94.159871 43.08461 93.683666 41.934951 92.836136 41.08742 \nC 91.988605 40.239889 90.838946 39.763685 89.640356 39.763685 \nC 88.441767 39.763685 87.292107 40.239889 86.444576 41.08742 \nC 85.597046 41.934951 85.120841 43.08461 85.120841 44.2832 \nC 85.120841 45.481789 85.597046 46.631448 86.444576 47.478979 \nC 87.292107 48.32651 88.441767 48.802714 89.640356 48.802714 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_8\">\n    <path d=\"M 89.640356 101.182734 \nC 92.437065 101.182734 95.119604 100.07159 97.097175 98.094018 \nC 99.074747 96.116447 100.185891 93.433907 100.185891 90.637199 \nC 100.185891 87.84049 99.074747 85.157951 97.097175 83.18038 \nC 95.119604 81.202808 92.437065 80.091664 89.640356 80.091664 \nC 86.843648 80.091664 84.161108 81.202808 82.183537 83.18038 \nC 80.205965 85.157951 79.094821 87.84049 79.094821 90.637199 \nC 79.094821 93.433907 80.205965 96.116447 82.183537 98.094018 \nC 84.161108 100.07159 86.843648 101.182734 89.640356 101.182734 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_9\">\n    <path d=\"M 135.994356 95.156714 \nC 137.192945 95.156714 138.342605 94.680509 139.190135 93.832979 \nC 140.037666 92.985448 140.51387 91.835788 140.51387 90.637199 \nC 140.51387 89.43861 140.037666 88.28895 139.190135 87.441419 \nC 138.342605 86.593889 137.192945 86.117684 135.994356 86.117684 \nC 134.795766 86.117684 133.646107 86.593889 132.798576 87.441419 \nC 131.951045 88.28895 131.474841 89.43861 131.474841 90.637199 \nC 131.474841 91.835788 131.951045 92.985448 132.798576 93.832979 \nC 133.646107 94.680509 134.795766 95.156714 135.994356 95.156714 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_10\">\n    <path d=\"M 135.994356 147.536733 \nC 138.791064 147.536733 141.473603 146.425589 143.451175 144.448017 \nC 145.428746 142.470446 146.53989 139.787907 146.53989 136.991198 \nC 146.53989 134.19449 145.428746 131.511951 143.451175 129.534379 \nC 141.473603 127.556807 138.791064 126.445663 135.994356 126.445663 \nC 133.197647 126.445663 130.515108 127.556807 128.537536 129.534379 \nC 126.559965 131.511951 125.448821 134.19449 125.448821 136.991198 \nC 125.448821 139.787907 126.559965 142.470446 128.537536 144.448017 \nC 130.515108 146.425589 133.197647 147.536733 135.994356 147.536733 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_11\">\n    <path d=\"M 182.348355 141.510713 \nC 183.546944 141.510713 184.696604 141.034509 185.544135 140.186978 \nC 186.391665 139.339447 186.86787 138.189788 186.86787 136.991198 \nC 186.86787 135.792609 186.391665 134.642949 185.544135 133.795419 \nC 184.696604 132.947888 183.546944 132.471683 182.348355 132.471683 \nC 181.149766 132.471683 180.000106 132.947888 179.152575 133.795419 \nC 178.305045 134.642949 177.82884 135.792609 177.82884 136.991198 \nC 177.82884 138.189788 178.305045 139.339447 179.152575 140.186978 \nC 180.000106 141.034509 181.149766 141.510713 182.348355 141.510713 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_12\">\n    <path d=\"M 182.348355 193.890733 \nC 185.145063 193.890733 187.827603 192.779588 189.805174 190.802017 \nC 191.782746 188.824445 192.89389 186.141906 192.89389 183.345198 \nC 192.89389 180.548489 191.782746 177.86595 189.805174 175.888378 \nC 187.827603 173.910807 185.145063 172.799663 182.348355 172.799663 \nC 179.551646 172.799663 176.869107 173.910807 174.891536 175.888378 \nC 172.913964 177.86595 171.80282 180.548489 171.80282 183.345198 \nC 171.80282 186.141906 172.913964 188.824445 174.891536 190.802017 \nC 176.869107 192.779588 179.551646 193.890733 182.348355 193.890733 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_13\">\n    <path d=\"M 228.702354 187.864713 \nC 229.900944 187.864713 231.050603 187.388508 231.898134 186.540977 \nC 232.745665 185.693447 233.221869 184.543787 233.221869 183.345198 \nC 233.221869 182.146608 232.745665 180.996949 231.898134 180.149418 \nC 231.050603 179.301887 229.900944 178.825683 228.702354 178.825683 \nC 227.503765 178.825683 226.354105 179.301887 225.506575 180.149418 \nC 224.659044 180.996949 224.182839 182.146608 224.182839 183.345198 \nC 224.182839 184.543787 224.659044 185.693447 225.506575 186.540977 \nC 226.354105 187.388508 227.503765 187.864713 228.702354 187.864713 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_14\">\n    <path d=\"M 228.702354 240.244732 \nC 231.499063 240.244732 234.181602 239.133588 236.159173 237.156016 \nC 238.136745 235.178445 239.247889 232.495906 239.247889 229.699197 \nC 239.247889 226.902489 238.136745 224.219949 236.159173 222.242378 \nC 234.181602 220.264806 231.499063 219.153662 228.702354 219.153662 \nC 225.905646 219.153662 223.223107 220.264806 221.245535 222.242378 \nC 219.267964 224.219949 218.156819 226.902489 218.156819 229.699197 \nC 218.156819 232.495906 219.267964 235.178445 221.245535 237.156016 \nC 223.223107 239.133588 225.905646 240.244732 228.702354 240.244732 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_15\">\n    <path d=\"M 275.056354 234.218712 \nC 276.254943 234.218712 277.404603 233.742507 278.252133 232.894977 \nC 279.099664 232.047446 279.575869 230.897786 279.575869 229.699197 \nC 279.575869 228.500608 279.099664 227.350948 278.252133 226.503417 \nC 277.404603 225.655887 276.254943 225.179682 275.056354 225.179682 \nC 273.857764 225.179682 272.708105 225.655887 271.860574 226.503417 \nC 271.013043 227.350948 270.536839 228.500608 270.536839 229.699197 \nC 270.536839 230.897786 271.013043 232.047446 271.860574 232.894977 \nC 272.708105 233.742507 273.857764 234.218712 275.056354 234.218712 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_16\">\n    <path d=\"M 275.056354 54.828734 \nC 277.853062 54.828734 280.535601 53.71759 282.513173 51.740019 \nC 284.490744 49.762447 285.601889 47.079908 285.601889 44.2832 \nC 285.601889 41.486491 284.490744 38.803952 282.513173 36.82638 \nC 280.535601 34.848809 277.853062 33.737665 275.056354 33.737665 \nC 272.259645 33.737665 269.577106 34.848809 267.599534 36.82638 \nC 265.621963 38.803952 264.510819 41.486491 264.510819 44.2832 \nC 264.510819 47.079908 265.621963 49.762447 267.599534 51.740019 \nC 269.577106 53.71759 272.259645 54.828734 275.056354 54.828734 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_17\">\n    <path d=\"M 367.764352 234.218712 \nC 368.962942 234.218712 370.112601 233.742507 370.960132 232.894977 \nC 371.807663 232.047446 372.283867 230.897786 372.283867 229.699197 \nC 372.283867 228.500608 371.807663 227.350948 370.960132 226.503417 \nC 370.112601 225.655887 368.962942 225.179682 367.764352 225.179682 \nC 366.565763 225.179682 365.416103 225.655887 364.568573 226.503417 \nC 363.721042 227.350948 363.244837 228.500608 363.244837 229.699197 \nC 363.244837 230.897786 363.721042 232.047446 364.568573 232.894977 \nC 365.416103 233.742507 366.565763 234.218712 367.764352 234.218712 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_18\">\n    <path d=\"M 367.764352 101.182734 \nC 370.561061 101.182734 373.2436 100.07159 375.221172 98.094018 \nC 377.198743 96.116447 378.309887 93.433907 378.309887 90.637199 \nC 378.309887 87.84049 377.198743 85.157951 375.221172 83.18038 \nC 373.2436 81.202808 370.561061 80.091664 367.764352 80.091664 \nC 364.967644 80.091664 362.285105 81.202808 360.307533 83.18038 \nC 358.329962 85.157951 357.218818 87.84049 357.218818 90.637199 \nC 357.218818 93.433907 358.329962 96.116447 360.307533 98.094018 \nC 362.285105 100.07159 364.967644 101.182734 367.764352 101.182734 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_19\">\n    <path d=\"M 414.118352 187.864713 \nC 415.316941 187.864713 416.466601 187.388508 417.314131 186.540977 \nC 418.161662 185.693447 418.637867 184.543787 418.637867 183.345198 \nC 418.637867 182.146608 418.161662 180.996949 417.314131 180.149418 \nC 416.466601 179.301887 415.316941 178.825683 414.118352 178.825683 \nC 412.919762 178.825683 411.770103 179.301887 410.922572 180.149418 \nC 410.075041 180.996949 409.598837 182.146608 409.598837 183.345198 \nC 409.598837 184.543787 410.075041 185.693447 410.922572 186.540977 \nC 411.770103 187.388508 412.919762 187.864713 414.118352 187.864713 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_20\">\n    <path d=\"M 414.118352 240.244732 \nC 416.91506 240.244732 419.597599 239.133588 421.575171 237.156016 \nC 423.552743 235.178445 424.663887 232.495906 424.663887 229.699197 \nC 424.663887 226.902489 423.552743 224.219949 421.575171 222.242378 \nC 419.597599 220.264806 416.91506 219.153662 414.118352 219.153662 \nC 411.321643 219.153662 408.639104 220.264806 406.661533 222.242378 \nC 404.683961 224.219949 403.572817 226.902489 403.572817 229.699197 \nC 403.572817 232.495906 404.683961 235.178445 406.661533 237.156016 \nC 408.639104 239.133588 411.321643 240.244732 414.118352 240.244732 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_21\">\n    <path d=\"M 460.472351 141.510713 \nC 461.670941 141.510713 462.8206 141.034509 463.668131 140.186978 \nC 464.515662 139.339447 464.991866 138.189788 464.991866 136.991198 \nC 464.991866 135.792609 464.515662 134.642949 463.668131 133.795419 \nC 462.8206 132.947888 461.670941 132.471683 460.472351 132.471683 \nC 459.273762 132.471683 458.124102 132.947888 457.276572 133.795419 \nC 456.429041 134.642949 455.952836 135.792609 455.952836 136.991198 \nC 455.952836 138.189788 456.429041 139.339447 457.276572 140.186978 \nC 458.124102 141.034509 459.273762 141.510713 460.472351 141.510713 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_22\">\n    <path d=\"M 460.472351 193.890733 \nC 463.26906 193.890733 465.951599 192.779588 467.92917 190.802017 \nC 469.906742 188.824445 471.017886 186.141906 471.017886 183.345198 \nC 471.017886 180.548489 469.906742 177.86595 467.92917 175.888378 \nC 465.951599 173.910807 463.26906 172.799663 460.472351 172.799663 \nC 457.675643 172.799663 454.993104 173.910807 453.015532 175.888378 \nC 451.03796 177.86595 449.926816 180.548489 449.926816 183.345198 \nC 449.926816 186.141906 451.03796 188.824445 453.015532 190.802017 \nC 454.993104 192.779588 457.675643 193.890733 460.472351 193.890733 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_23\">\n    <path d=\"M 506.826351 95.156714 \nC 508.02494 95.156714 509.1746 94.680509 510.02213 93.832979 \nC 510.869661 92.985448 511.345866 91.835788 511.345866 90.637199 \nC 511.345866 89.43861 510.869661 88.28895 510.02213 87.441419 \nC 509.1746 86.593889 508.02494 86.117684 506.826351 86.117684 \nC 505.627761 86.117684 504.478102 86.593889 503.630571 87.441419 \nC 502.78304 88.28895 502.306836 89.43861 502.306836 90.637199 \nC 502.306836 91.835788 502.78304 92.985448 503.630571 93.832979 \nC 504.478102 94.680509 505.627761 95.156714 506.826351 95.156714 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 1.5; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"patch_24\">\n    <path d=\"M 506.826351 147.536733 \nC 509.623059 147.536733 512.305598 146.425589 514.28317 144.448017 \nC 516.260741 142.470446 517.371885 139.787907 517.371885 136.991198 \nC 517.371885 134.19449 516.260741 131.511951 514.28317 129.534379 \nC 512.305598 127.556807 509.623059 126.445663 506.826351 126.445663 \nC 504.029642 126.445663 501.347103 127.556807 499.369531 129.534379 \nC 497.39196 131.511951 496.280816 134.19449 496.280816 136.991198 \nC 496.280816 139.787907 497.39196 142.470446 499.369531 144.448017 \nC 501.347103 146.425589 504.029642 147.536733 506.826351 147.536733 \nz\n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: #6fa4ff; stroke: #6fa4ff; stroke-width: 2; stroke-linejoin: miter\"/>\n   </g>\n   <g id=\"text_1\">\n    <g clip-path=\"url(#pbb971af4d8)\">\n     <!-- ${q}_{0}$ -->\n     <g transform=\"translate(37.300897 48.734176) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-Oblique-71\" d=\"M 2669 525 \nQ 2438 222 2123 65 \nQ 1809 -91 1428 -91 \nQ 897 -91 595 267 \nQ 294 625 294 1253 \nQ 294 1759 480 2231 \nQ 666 2703 1013 3078 \nQ 1238 3322 1530 3453 \nQ 1822 3584 2144 3584 \nQ 2531 3584 2781 3431 \nQ 3031 3278 3144 2969 \nL 3244 3494 \nL 3822 3494 \nL 2888 -1319 \nL 2309 -1319 \nL 2669 525 \nz\nM 891 1338 \nQ 891 875 1084 633 \nQ 1278 391 1644 391 \nQ 2188 391 2572 911 \nQ 2956 1431 2956 2175 \nQ 2956 2625 2757 2864 \nQ 2559 3103 2188 3103 \nQ 1916 3103 1684 2976 \nQ 1453 2850 1281 2606 \nQ 1100 2350 995 2006 \nQ 891 1663 891 1338 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_2\">\n    <g clip-path=\"url(#pbb971af4d8)\">\n     <!-- ${q}_{1}$ -->\n     <g transform=\"translate(37.300897 95.088175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_3\">\n    <g clip-path=\"url(#pbb971af4d8)\">\n     <!-- ${q}_{2}$ -->\n     <g transform=\"translate(37.300897 141.442175) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_4\">\n    <g clip-path=\"url(#pbb971af4d8)\">\n     <!-- ${q}_{3}$ -->\n     <g transform=\"translate(37.300897 187.796174) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"text_5\">\n    <g clip-path=\"url(#pbb971af4d8)\">\n     <!-- ${q}_{4}$ -->\n     <g transform=\"translate(37.300897 234.150174) scale(0.1625 -0.1625)\">\n      <defs>\n       <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-Oblique-71\"/>\n      <use xlink:href=\"#DejaVuSans-34\" transform=\"translate(63.476562 -16.40625) scale(0.7)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_15\">\n    <path d=\"M 89.640356 96.663219 \nL 89.640356 84.611179 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_16\">\n    <path d=\"M 83.614336 90.637199 \nL 95.666376 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_17\">\n    <path d=\"M 135.994356 143.017218 \nL 135.994356 130.965178 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_18\">\n    <path d=\"M 129.968336 136.991198 \nL 142.020375 136.991198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 182.348355 189.371218 \nL 182.348355 177.319178 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 176.322335 183.345198 \nL 188.374375 183.345198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_21\">\n    <path d=\"M 228.702354 235.725217 \nL 228.702354 223.673177 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_22\">\n    <path d=\"M 222.676334 229.699197 \nL 234.728374 229.699197 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_23\">\n    <path d=\"M 275.056354 50.309219 \nL 275.056354 38.25718 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_24\">\n    <path d=\"M 269.030334 44.2832 \nL 281.082374 44.2832 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_25\">\n    <path d=\"M 321.410353 21.1062 \nL 321.410353 67.460199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_26\">\n    <path d=\"M 321.410353 67.460199 \nL 321.410353 113.814199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_27\">\n    <path d=\"M 321.410353 113.814199 \nL 321.410353 160.168198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_28\">\n    <path d=\"M 321.410353 160.168198 \nL 321.410353 206.522197 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_29\">\n    <path d=\"M 321.410353 206.522197 \nL 321.410353 252.876197 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke-dasharray: 3.7,1.6; stroke-dashoffset: 0; stroke: #000000\"/>\n   </g>\n   <g id=\"line2d_30\">\n    <path d=\"M 367.764352 96.663219 \nL 367.764352 84.611179 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_31\">\n    <path d=\"M 361.738333 90.637199 \nL 373.790372 90.637199 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_32\">\n    <path d=\"M 414.118352 235.725217 \nL 414.118352 223.673177 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_33\">\n    <path d=\"M 408.092332 229.699197 \nL 420.144372 229.699197 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_34\">\n    <path d=\"M 460.472351 189.371218 \nL 460.472351 177.319178 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_35\">\n    <path d=\"M 454.446331 183.345198 \nL 466.498371 183.345198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_36\">\n    <path d=\"M 506.826351 143.017218 \nL 506.826351 130.965178 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_37\">\n    <path d=\"M 500.800331 136.991198 \nL 512.852371 136.991198 \n\" clip-path=\"url(#pbb971af4d8)\" style=\"fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: square\"/>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"pbb971af4d8\">\n   <rect x=\"7.2\" y=\"7.2\" width=\"530.21999\" height=\"250.311597\"/>\n  </clipPath>\n </defs>\n</svg>\n",
      "text/plain": [
       "<Figure size 956.385x451.5 with 1 Axes>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(5)\n",
    "cir.cnot_ring()\n",
    "cir.barrier()\n",
    "cir.cnot_ring(minmax=[1,4], step=3, reverse=True)\n",
    "cir.draw()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 振幅编码\n",
    "\n",
    "下面我们展示一个振幅编码的例子，先准备一些数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "nqubit = 4\n",
    "batch = 2\n",
    "data = torch.randn(batch, 2 ** nqubit)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后构建量子线路，并通过`observable`指定测量线路和测量基底。测量线路和测量基底也可以使用列表形式的组合，如`wires=[0,1,2]`、`basis='xyz'`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "cir = dq.QubitCircuit(nqubit)\n",
    "cir.rxlayer()\n",
    "cir.cnot_ring()\n",
    "cir.observable(wires=0, basis='z')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "量子门和观测量分别被记录在`operators`和`observables`中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "QubitCircuit(\n",
      "  (operators): Sequential(\n",
      "    (0): RxLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rx(wires=[0], theta=2.4975132942199707)\n",
      "        (1): Rx(wires=[1], theta=3.552344560623169)\n",
      "        (2): Rx(wires=[2], theta=7.044006824493408)\n",
      "        (3): Rx(wires=[3], theta=0.7623158097267151)\n",
      "      )\n",
      "    )\n",
      "    (1): CnotRing(\n",
      "      (gates): Sequential(\n",
      "        (0): CNOT(wires=[0, 1])\n",
      "        (1): CNOT(wires=[1, 2])\n",
      "        (2): CNOT(wires=[2, 3])\n",
      "        (3): CNOT(wires=[3, 0])\n",
      "      )\n",
      "    )\n",
      "  )\n",
      "  (observables): ModuleList(\n",
      "    (0): Observable(\n",
      "      (gates): Sequential(\n",
      "        (0): PauliZ(wires=[0])\n",
      "      )\n",
      "    )\n",
      "  )\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "print(cir)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "振幅编码会自动补0或者舍弃多余的数据，以及进行归一化。\n",
    "\n",
    "通过线路的`forward()`得到末态，`forward()`有`data`和`state`两个参数，分别对应放入量子门的数据，以及线路作用的初态，即分别对应角度编码和振幅编码。\n",
    "\n",
    "测量期望，输出的形状为(batch, 观测量的数量)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 16, 1])\n",
      "tensor([[1.],\n",
      "        [1.]], grad_fn=<LinalgVectorNormBackward0>)\n",
      "tensor([[-0.1803],\n",
      "        [-0.0892]], grad_fn=<StackBackward0>)\n"
     ]
    }
   ],
   "source": [
    "state = cir.amplitude_encoding(data)\n",
    "state = cir(state=state)\n",
    "exp = cir.expectation()\n",
    "print(state.shape)\n",
    "print(state.norm(dim=-2))\n",
    "print(exp)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 角度编码\n",
    "\n",
    "角度编码只需要对相应的Gate或Layer指定`encode=True`，会自动将数据的特征依次加入编码层，多余的会被舍弃。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.0000,  0.8415,  0.9093,  0.1411],\n",
      "        [-0.7568, -0.9589, -0.2794,  0.6570]])\n"
     ]
    }
   ],
   "source": [
    "nqubit = 4\n",
    "batch = 2\n",
    "data = torch.sin(torch.tensor(list(range(batch * nqubit)))).reshape(batch, nqubit)\n",
    "print(data)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这次我们对每条线路都进行一次测量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 16, 1])\n",
      "tensor([[1.0000],\n",
      "        [1.0000]])\n",
      "tensor([[1.4901e-08, 0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
      "        [7.4506e-09, 7.4506e-09, 0.0000e+00, 0.0000e+00]])\n"
     ]
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(nqubit)\n",
    "cir.hlayer()\n",
    "cir.rxlayer(encode=True)\n",
    "cir.cnot_ring()\n",
    "for i in range(nqubit):\n",
    "    cir.observable(wires=i)\n",
    "state = cir(data)\n",
    "exp = cir.expectation()\n",
    "print(state.shape)\n",
    "print(state.norm(dim=-2))\n",
    "print(exp)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`QubitCircuit`支持data re-uploading，只需要初始化时指定`reupload=True`，数据就会被循环地放入线路中。\n",
    "\n",
    "补充说明：`encode`只能针对一条一维的数据，线路对batch的支持是通过`torch.vmap`，并且计算完一次前向过程会自动初始化`encoders`，因为量子门无法保存多组参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "QubitCircuit(\n",
      "  (operators): Sequential(\n",
      "    (0): RxLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rx(wires=[0], theta=tensor([0.]))\n",
      "        (1): Rx(wires=[1], theta=tensor([0.8415]))\n",
      "        (2): Rx(wires=[2], theta=tensor([0.9093]))\n",
      "        (3): Rx(wires=[3], theta=tensor([0.1411]))\n",
      "      )\n",
      "    )\n",
      "    (1): CnotRing(\n",
      "      (gates): Sequential(\n",
      "        (0): CNOT(wires=[0, 1])\n",
      "        (1): CNOT(wires=[1, 2])\n",
      "        (2): CNOT(wires=[2, 3])\n",
      "        (3): CNOT(wires=[3, 0])\n",
      "      )\n",
      "    )\n",
      "    (2): RxLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rx(wires=[0], theta=tensor([0.]))\n",
      "        (1): Rx(wires=[1], theta=tensor([0.8415]))\n",
      "        (2): Rx(wires=[2], theta=tensor([0.9093]))\n",
      "        (3): Rx(wires=[3], theta=tensor([0.1411]))\n",
      "      )\n",
      "    )\n",
      "    (3): CnotRing(\n",
      "      (gates): Sequential(\n",
      "        (0): CNOT(wires=[0, 1])\n",
      "        (1): CNOT(wires=[1, 2])\n",
      "        (2): CNOT(wires=[2, 3])\n",
      "        (3): CNOT(wires=[3, 0])\n",
      "      )\n",
      "    )\n",
      "  )\n",
      "  (observables): ModuleList()\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "cir = dq.QubitCircuit(nqubit, reupload=True)\n",
    "cir.rxlayer(encode=True)\n",
    "cir.cnot_ring()\n",
    "cir.rxlayer(encode=True)\n",
    "cir.cnot_ring()\n",
    "cir.encode(data[0])\n",
    "print(cir)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 混合量子-经典模型\n",
    "\n",
    "DeepQuantum基于PyTorch，能够方便自然地实现量子模型和经典模型的混合计算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('fc.weight', tensor([[-0.0471,  0.2069,  0.2709,  0.1491, -0.3424, -0.1332,  0.1634, -0.3425],\n",
      "        [-0.2075,  0.2382,  0.2312, -0.1837,  0.0663, -0.3311,  0.2698, -0.1313],\n",
      "        [-0.1981, -0.0585,  0.0302,  0.0370, -0.0251, -0.0460, -0.1088, -0.1174],\n",
      "        [ 0.1519, -0.1392, -0.0941,  0.2542,  0.1750,  0.1514,  0.2043, -0.3521]])), ('fc.bias', tensor([ 0.1420,  0.2372, -0.0617,  0.2729])), ('cir.init_state', tensor([[1.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j],\n",
      "        [0.+0.j]])), ('cir.operators.0.gates.0.matrix', tensor([[ 0.7071+0.j,  0.7071+0.j],\n",
      "        [ 0.7071+0.j, -0.7071+0.j]])), ('cir.operators.0.gates.1.matrix', tensor([[ 0.7071+0.j,  0.7071+0.j],\n",
      "        [ 0.7071+0.j, -0.7071+0.j]])), ('cir.operators.0.gates.2.matrix', tensor([[ 0.7071+0.j,  0.7071+0.j],\n",
      "        [ 0.7071+0.j, -0.7071+0.j]])), ('cir.operators.0.gates.3.matrix', tensor([[ 0.7071+0.j,  0.7071+0.j],\n",
      "        [ 0.7071+0.j, -0.7071+0.j]])), ('cir.operators.1.gates.0.theta', tensor(1.0337)), ('cir.operators.1.gates.1.theta', tensor(5.8421)), ('cir.operators.1.gates.2.theta', tensor(1.2571)), ('cir.operators.1.gates.3.theta', tensor(9.4017)), ('cir.operators.2.gates.0.matrix', tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])), ('cir.operators.2.gates.1.matrix', tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])), ('cir.operators.2.gates.2.matrix', tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])), ('cir.operators.2.gates.3.matrix', tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],\n",
      "        [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j]])), ('cir.observables.0.gates.0.matrix', tensor([[ 1.+0.j,  0.+0.j],\n",
      "        [ 0.+0.j, -1.+0.j]])), ('cir.observables.1.gates.0.matrix', tensor([[ 1.+0.j,  0.+0.j],\n",
      "        [ 0.+0.j, -1.+0.j]])), ('cir.observables.2.gates.0.matrix', tensor([[ 1.+0.j,  0.+0.j],\n",
      "        [ 0.+0.j, -1.+0.j]])), ('cir.observables.3.gates.0.matrix', tensor([[ 1.+0.j,  0.+0.j],\n",
      "        [ 0.+0.j, -1.+0.j]]))])\n",
      "y tensor([[7.4506e-09, 7.4506e-09, 0.0000e+00, 0.0000e+00],\n",
      "        [7.4506e-09, 7.4506e-09, 0.0000e+00, 0.0000e+00]],\n",
      "       grad_fn=<StackBackward0>)\n"
     ]
    }
   ],
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self, dim_in, nqubit) -> None:\n",
    "        super().__init__()\n",
    "        self.fc = nn.Linear(dim_in, nqubit)\n",
    "        self.cir = self.circuit(nqubit)\n",
    "\n",
    "    def circuit(self, nqubit):\n",
    "        cir = dq.QubitCircuit(nqubit)\n",
    "        cir.hlayer()\n",
    "        cir.rxlayer(encode=True)\n",
    "        cir.cnot_ring()\n",
    "        for i in range(nqubit):\n",
    "            cir.observable(wires=i)\n",
    "        return cir\n",
    "    \n",
    "    def forward(self, x):\n",
    "        x = torch.arctan(self.fc(x))\n",
    "        self.cir(x)\n",
    "        exp = self.cir.expectation()\n",
    "        return exp\n",
    "\n",
    "nqubit = 4\n",
    "batch = 2\n",
    "nfeat = 8\n",
    "x = torch.sin(torch.tensor(list(range(batch * nfeat)))).reshape(batch, nfeat)\n",
    "net = Net(nfeat, nqubit)\n",
    "y = net(x)\n",
    "print(net.state_dict())\n",
    "print('y', y)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线路拼接以及更灵活地使用数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "nqubit = 2\n",
    "batch = 2\n",
    "data1 = torch.sin(torch.tensor(list(range(batch * nqubit)))).reshape(batch, nqubit)\n",
    "data2 = torch.cos(torch.tensor(list(range(batch * nqubit)))).reshape(batch, nqubit)\n",
    "cir1 = dq.QubitCircuit(nqubit)\n",
    "cir1.rxlayer(encode=True)\n",
    "cir2 = dq.QubitCircuit(nqubit)\n",
    "cir2.rylayer(encode=True)\n",
    "cir3 = dq.QubitCircuit(nqubit)\n",
    "cir3.rzlayer()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过线路加法来共享变分参数。\n",
    "\n",
    "注意，不建议对encoder部分进行复杂的线路加法来共享数据，因为需要保证数据的顺序与encoders完全一致。一旦出现错位，由于共享了encoders，会造成全局的影响。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "QubitCircuit(\n",
      "  (operators): Sequential(\n",
      "    (0): RxLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rx(wires=[0], theta=tensor([0.]))\n",
      "        (1): Rx(wires=[1], theta=tensor([0.8415]))\n",
      "      )\n",
      "    )\n",
      "    (1): RzLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rz(wires=[0], theta=9.046151161193848)\n",
      "        (1): Rz(wires=[1], theta=7.990583896636963)\n",
      "      )\n",
      "    )\n",
      "    (2): RyLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Ry(wires=[0], theta=tensor([1.]))\n",
      "        (1): Ry(wires=[1], theta=tensor([0.5403]))\n",
      "      )\n",
      "    )\n",
      "    (3): RzLayer(\n",
      "      (gates): Sequential(\n",
      "        (0): Rz(wires=[0], theta=9.046151161193848)\n",
      "        (1): Rz(wires=[1], theta=7.990583896636963)\n",
      "      )\n",
      "    )\n",
      "  )\n",
      "  (observables): ModuleList(\n",
      "    (0): Observable(\n",
      "      (gates): Sequential(\n",
      "        (0): PauliZ(wires=[0])\n",
      "      )\n",
      "    )\n",
      "  )\n",
      ")\n",
      "tensor([[0.5403],\n",
      "        [0.6798]], grad_fn=<StackBackward0>)\n"
     ]
    }
   ],
   "source": [
    "data = torch.cat([data1, data2], dim=-1)\n",
    "cir = cir1 + cir3 + cir2 + cir3\n",
    "cir.observable(0)\n",
    "cir.encode(data[0])\n",
    "print(cir)\n",
    "cir(data)\n",
    "print(cir.expectation())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的结果也可以由多个线路的分段演化得到。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.5403],\n",
      "        [0.6798]], grad_fn=<StackBackward0>)\n"
     ]
    }
   ],
   "source": [
    "state = cir1(data1)\n",
    "state = cir3(state=state)\n",
    "state = cir2(data2, state=state)\n",
    "state = cir3(state=state)\n",
    "cir3.reset_observable()\n",
    "cir3.observable(0)\n",
    "print(cir3.expectation())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这种方式当然就可以更灵活地使用数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.5537],\n",
      "        [-0.2558]], grad_fn=<StackBackward0>)\n"
     ]
    }
   ],
   "source": [
    "state = cir1(data1)\n",
    "state = cir2(data2, state=state)\n",
    "state = cir3(state=state)\n",
    "state = cir1(data2, state=state)\n",
    "state = cir2(data1, state=state)\n",
    "cir2.reset_observable()\n",
    "cir2.observable(0)\n",
    "print(cir2.expectation())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dq",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
